Home » excel » c# – Excel CodeModule doesn't save added code

c# – Excel CodeModule doesn't save added code

Posted by: admin May 14, 2020 Leave a comment


I have hundreds of excel files that need to be extended with code and new sheets. When I add the code first, it saves correctly. Unfortunately the code contains references to the sheets that needs to be added. So I have to add the sheet before… But then the problem occurs, that the added code is not saved within the workbook. Even if added the sheet manually… I’m not able to add code in any way.

How to&Answers:

The problem was solved when I used SaveAs instead of Save and provided the file format:

workbook.SaveAs (filename, XlFileFormat.xlOpenXMLWorkbookMacroEnabled);

The excel file extension doesn’t matter. Worked for me 🙂


Also the file extension must match file format. For instance, files saved with XlFileFormat.xlOpenXMLWorkbookMacroEnabled must be saved with the xlsm extension. Otherwise, every time on opening the file, a alert message will appear.