Does the XlsIO component support Excel spreadsheets that have VBA code in them?
Syncfusion .NET Excel library supports preserving macros from input Excel files and saves them as macro enabled files. However, it does not support creation of Excel files with macros or VBA.
Steps to open and save Excel files with macros:
Step 1: Create a macro using Microsoft Excel application and save the file.
Step 2: Open the saved file using Syncfusion Essential XlsIO.
- Create a new C# console application project in Visual Studio and install the Syncfusion.XlsIO.WinForms NuGet package as reference to your application from NuGet.org.
- Include the following namespaces in Program.cs file.
C#
using Syncfusion.XlsIO;
VB.NET
Imports Syncfusion.XlsIO
- Include the following code snippet in the main method of Program.cs file to open the macro file created using Microsoft Excel and save it using Syncfusion Essential XlsIO with some changes in the worksheet.
C#
//Create an instance of ExcelEngine using (ExcelEngine excelEngine = new ExcelEngine()) { //Instantiate the Excel application object IApplication application = excelEngine.Excel; //Set the application default version application.DefaultVersion = ExcelVersion.Excel2016; //Load an existing Excel macro workbook into IWorkbook IWorkbook workbook = application.Workbooks.Open("../../Sample.xlsm"); //Get the first worksheet in the workbook into IWorksheet IWorksheet worksheet = workbook.Worksheets[0]; //Modify the cell content in the worksheet worksheet.Range["A2"].Text = "Cell content modified using XlsIO"; worksheet.Range["A2"].CellStyle.Font.Bold = true; //Save the Excel document workbook.SaveAs("Output.xlsm"); }
VB.NET
'Create an instance of ExcelEngine Using excelEngine As ExcelEngine = New ExcelEngine() 'Instantiate the Excel application object Dim application As IApplication = excelEngine.Excel 'Set the application default version application.DefaultVersion = ExcelVersion.Excel2016 'Load an existing Excel macro workbook into IWorkbook Dim workbook As IWorkbook = application.Workbooks.Open("../../Sample.xlsm") 'Get the first worksheet in the workbook into IWorksheet Dim worksheet As IWorksheet = workbook.Worksheets(0) 'Modify the cell content in the worksheet worksheet.Range("A2").Text = "Cell content modified using XlsIO" worksheet.Range("A2").CellStyle.Font.Bold = True 'Save the Excel document workbook.SaveAs("Output.xlsm") End Using
A complete working sample to open a macro enabled Excel document and resaving it with some changes, can be downloaded from Open and Save Macro Enabled Excel file.zip.
Support for editing macros in Excel document will be provided in our future releases.