How to Load and Save Macro Enabled Excel Files With OpenXMLMacro in Excel for WinForms

When you have a repetitive Excel task you want to automate, you turn to Macros: a code written in VBA that can be inserted into Excel files to perform specific tasks like inserting text on a button click or performing calculations that cannot be done using in-built Excel functions. Excel for WinForms now supports OpenXMLMacro file format, with which you can save and load Macros-enabled Excel files in C1Excel. Alternately, you can also load and save such files from and to .xlsm file extension. The VBA Code is loaded in Binary format into xlsm files. If you're **saving your macro-enabled files to '.xlsm',** you don't need to specify FileFormat OpenXMLMacro to save the file. You can save the file using following code:


Alternately, if you want to save macro enabled files to a stream or any other excel extension, you can save the file using:

c1XLBook1.Save(xlsFileName, FileFormat.OpenXMLMacro);  

Here's an example of how C1Excel loads and saves Macro-enabled Excel file. ComponentOne Studio Excel for WinForms OpenXMLMacro The Macro calculates Last Day of a Month from a Date column. C1Excel loads and saves file using following code

// Copy Macro enabled Excel File  
string xlsFileName = "..\\\..\\\XlsxTestBin.xlsm";  
c1XLBook1.Sheets[0][0, 0].Value = "It is copy!";  
xlsFileName = xlsFileName.Replace(".xlsm", "Modified.xlsm");  

Open the Modified.xlsm and you'll find the Macros preserved. Note: C1Excel can load and save only opaque copy of Macro code. Also, C1Excel does not support any properties to add VBA code. Macros will be deleted if you save a file having macros to .xlsx/xls file.

Already have a subscription? Go to C1Studio to update your version!
Download your 30-day trial to try out OpenXMLMacro in Excel >>
Read more about Excel for WinForms>>


GrapeCity Developer Tools
comments powered by Disqus