1

I have a C# program that runs a macro existing inside an xlsm file and after this macro has finished its operations, I would like to save this xlsm as .xlsx file. This is the code that runs and saves the macro file as xlsx:

void RunMacro(string macroFilename, string xlsxFilePath)
{
    Excel.Application app = new Excel.Application();
    Excel.Workbook workbook;
    workbook = app.Workbooks.Open(macroFilename);
    app.Visible = true;
    app.Run("Main");
    workbook.SaveAs(xlsxFilePath, Excel.XlFileFormat.xlOpenXMLWorkbook);
    app.Workbooks.Close();
    app.Quit();
}

Now, the issue is that in the line workbook.SaveAs(), I will get a prompt like this from Excel: Following features cannot be saved in macro-free workbooks

So how can I make it programatically that "Yes" is the default option here and this prompt is automatically closed without needing to click on anything?

EDIT: The following threads (linked as the reason for closing this question) are not a solution:

How do I save an XLSM file as an XLSX file without prompting the user about lost data?

Save a *.xlsm file as *.xlsx and suppress the pop-up

Basically, there are two proposed solutions in these threads:

  1. Set application.DisplayAlerts = false and call workbook.SaveAs(xlsxFilePath, Excel.XlFileFormat.xlOpenXMLWorkbook); This still results in a prompt coming up where it is necessary to select 'Yes'. Here is my code:
void RunMacro(string macroFilename, string xlsxFilePath)
{
    Excel.Application app = new Excel.Application();
    Excel.Workbook workbook;
    workbook = app.Workbooks.Open(macroFilename);
    app.Visible = true;
    app.DisplayAlerts = false;
    app.Run("Main");
    workbook.SaveAs(xlsxFilePath,Excel.XlFileFormat.xlOpenXMLWorkbook);
    app.Workbooks.Close();
    app.Quit();
}
  1. Use Workbook.SaveCopyAs(xlsxFilePath)

This method call does circumvent the Excel prompt, but the xlsx file that it produces is corrupted: enter image description here

Here is the code:

void RunMacro(string macroFilename, string xlsxFilePath)
{
    Excel.Application app = new Excel.Application();
    Excel.Workbook workbook;
    workbook = app.Workbooks.Open(macroFilename);
    app.Visible = true;
    app.Run("Main");
    workbook.SaveCopyAs(xlsxFilePath);
    app.Workbooks.Close();
    app.Quit();
}
vinn23
  • 39
  • 4

0 Answers0