Interop.Excel version 12
Office Excel 2007
I have worked on code to open .xslm file and then converting (saving) it as .xslx. Between this, I do need to do some processing as well but it doesn’t matter. Following is the code:
namespace Exceltest
{
class Program
{
static void Main(string[] args)
{
Application oXL = null;
oXL = new Application();
oXL.Visible = false;
oXL.DisplayAlerts = false;
try
{
Workbook book = oXL.Workbooks.Open("E:\\CorrectPF1.xlsm", 0, true, 5, "access123", "", true,
XlPlatform.xlWindows, "\t", false, false, 0, true, null, XlCorruptLoad.xlNormalLoad);
Worksheet worksheet = (Worksheet)book.Sheets["Resource allocation"];
worksheet.Activate();
oXL.Visible = false;
oXL.DisplayAlerts = false;
book.SaveAs("E:\\PF1.xlsx", XlFileFormat.xlOpenXMLWorkbook,
"", Type.Missing, Type.Missing, Type.Missing, XlSaveAsAccessMode.xlNoChange,
XlSaveConflictResolution.xlLocalSessionChanges, Type.Missing, Type.Missing,
Type.Missing, false);
}
catch (Exception ex)
{
//Handling exception here
}
}
}
}
This code executes with no issue (without any exception) but No file is saved with .xslx extension. I found it is working for some set of files but not others. Say, it is not working for file named PF1.xslm. I am able to open PF1.xslm in Office Excel and save it as .xslx. So, the same suppose to work programmatically as well.
You can’t save files with macros in it to .xlsx format. If you save a file yourself you should get an error message. I suspect the macro just gives up and doesn’t save it, rather than displaying that error message.