We use cookies to give you the best experience on our website. If you continue to browse, then you agree to our privacy policy and cookie policy. (Last updated on: November 16, 2018).
Unfortunately, activation email could not send to your email. Please try again.
Syncfusion Feedback

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

Platform: WinForms |
Control: XlsIO |
Published Date: April 30, 2011 |
Last Revised Date: July 1, 2019

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 NuGet.org.
  2. Include the following namespaces in Program.cs file.

C#

using Syncfusion.XlsIO;

 

VB.NET

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.

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.

Note:

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

 

2X faster development

The ultimate WinForms UI toolkit to boost your development speed.
ADD COMMENT
You must log in to leave a comment

Please sign in to access our KB

This page will automatically be redirected to the sign-in page in 10 seconds.

Up arrow icon

Warning Icon You are using an outdated version of Internet Explorer that may not display all features of this and other websites. Upgrade to Internet Explorer 8 or newer for a better experience.Close Icon