Articles in this section
Category / Section

How Does WInForms XlsIO Support Excel Spreadsheets that Have VBA Code?

3 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 the creation of Excel files with macros or VBA in WinForm XLsIO.

Steps to open and save Excel files with macros:

Step 1: Create a macro using the 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 resave 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.

 

Conclusion

I hope you enjoyed learning about how does WInForms XlsIO support Excel spreadsheets that have VBA code.

You can refer to our XIsIO’s feature tour page to learn about its other groundbreaking features. Explore our UG documentation and online demos to understand how to manipulate data in Excel documents.

If you are an existing user, you can access our latest components from the License and Downloads page. For new users, you can try our 30-day free trial to check out XlsIO and other Syncfusion components.

If you have any queries or require clarification, please let us know in the comments below or contact us through our support forumsSupport Tickets, or feedback portal. We are always happy to assist you!

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