Articles in this section
Category / Section

Does the XlsIO component support Excel spreadsheets that have VBA code in them?

2 mins read

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.

  1. Create a new C# console application project in Visual Studio and install the Syncfusion.XlsIO.WinForms NuGet package as reference to your application from
  2. Include the following namespaces in Program.cs file.


using Syncfusion.XlsIO;



Imports Syncfusion.XlsIO


  1. 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.


//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



'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
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


Support for editing macros in Excel document will be provided in our future releases.


Did you find this information helpful?
Help us improve this page
Please provide feedback or comments
Comments (0)
Please sign in to leave a comment