Spire.XLS 16.3.6 supports manipulating the VBA Macro Project

Spire.XLS 16.3.6 supports manipulating the VBA Macro Project

2026-03-27 06:25:20

We're pleased to announce the release of Spire.XLS 16.3.6. This version supports manipulation of the VBA Macro Project and the Data Simulation Analysis (Scenario Manager). Besides, it also fixes an issue where macros were lost when copying worksheets. More details are shown below.

Here is a list of changes made in this release

Category ID Description
New Feature SPIREXLS-938 SPIREXLS-5995 Added support for VBA Macro Project manipulation, covering creation, reading, modification, and deletion.

Adding VBA project:

Workbook workbook = new Workbook();

// Add VBA project to the document
IVbaProject vbaProject = workbook.VbaProject;
vbaProject.Name = "SampleVBAMacro";

string text = "Encoding before modification: " + vbaProject.CodePage.ToString() + "\n";
vbaProject.CodePage = 936; // Set encoding, support Chinese

text += "Encoding after modification: " + vbaProject.CodePage.ToString() + "\n";
File.WriteAllText(outputFile_TXT, text);

// Add VBA module to the project
IVbaModule vbaModule = vbaProject.Modules.Add("SampleModule", VbaModuleType.Module);
// Set VBA macro source code
vbaModule.SourceCode = @"
Sub ExampleMacro()
    ' Declare variables
    Dim ws As Worksheet
    Dim i As Integer
    
    ' Set reference to the active worksheet 
    Set ws = ActiveSheet 
    
    ' Clear worksheet content (optional)
    ws.Cells.Clear
    
    ' Populate sample data
    With ws
        ' Write header row 
        .Range(""A1:C1"").Value = Array(""No."", ""Project Name"", ""Amount"")
        
        ' Loop to fill 10 rows of data 
        For i = 1 To 10
            .Cells(i + 1, 1).Value = i  ' No. column 
            .Cells(i + 1, 2).Value = ""Project "" & i  ' Project Name column 
            .Cells(i + 1, 3).Value = i * 100  ' Amount column (example calculation)
        Next i
        
        ' AutoFit column widths 
        .Columns(""A:C"").AutoFit
        
        ' Format header row
        With .Range(""A1:C1"")
            .Font.Bold = True
            .Interior.Color = RGB(200, 220, 255)  ' Light blue background
        End With
        
        ' Format amount column 
        .Range(""C2:C11"").NumberFormat = ""$#,##0.00""
    End With
    
    ' Show completion message
    MsgBox ""Data population completed!"", vbInformation, ""Operation Prompt""
End Sub";

// Save the Excel file
workbook.SaveToFile(outputFile_Xls, FileFormat.Version97to2003);

Reading VBA project:

Workbook wb = new Workbook();
wb.LoadFromFile(inputFile);
Worksheet ws = wb.Worksheets[0];
IVbaProject vbaProject = wb.VbaProject;

string text = "IsProtected:" + vbaProject.IsProtected + "\n";
text += "Name:" + vbaProject.Name + "\n";
text += "Description:" + vbaProject.Description + "\n";
text += "HelpFileName:" + vbaProject.HelpFileName + "\n";
text += "ConditionalCompilation:" + vbaProject.ConditionalCompilation + "\n";
text += "LockProjectView:" + vbaProject.LockProjectView + "\n";
text += "Password:" + vbaProject.Password + "\n";
text += "CodePage:" + vbaProject.CodePage + "\n";

IVbaModule mod = vbaProject.Modules.GetWorksheetModule(ws);
text += "IVbaModule:" + "\n";
text += "Name:" + mod.Name.ToString() + "\n";
text += "SourceCode:\n" + mod.SourceCode.ToString() + "\n";
text += "Type:" + mod.Type.ToString() + "\n";

File.WriteAllText(outputFile_TXT, text.ToString());
vbaProject.Modules.Clear();
wb.SaveToFile(outputFile);

Editing VBA project:

Workbook wb = new Workbook();
wb.LoadFromFile(inputFile);
Worksheet ws = wb.Worksheets[0];

IVbaProject vbaProject = wb.VbaProject;
vbaProject.Password = "1234";
vbaProject.Name = "modify";
vbaProject.Description = "Description";
vbaProject.HelpFileName = "image1.png";
vbaProject.ConditionalCompilation = "DEBUG = 2";
vbaProject.LockProjectView = true;

IVbaModule mod = vbaProject.Modules.GetWorksheetModule(ws);
mod.Name = "IVbaModule";
mod.SourceCode = "Dim lRow As Long";
mod.Type = VbaModuleType.Module;

wb.SaveToFile(outputFile);

Removing VBA project:

Workbook wb1 = new Workbook();
wb1.LoadFromFile(inputFile_1);
IVbaProject vbaProject1 = wb1.VbaProject;

vbaProject1.Modules.Remove("SampleModule");
vbaProject1.Modules.RemoveAt(0);
 
wb1.SaveToFile(outputFile_1);
New Feature SPIREXLS-6020 Added support for Data Simulation Analysis (Scenario Manager), covering creation, editing, deletion, summary generation, and merging.

Creating scenarios:

Workbook wb = new Workbook();
wb.LoadFromFile(inputFile);
Worksheet worksheet = wb.Worksheets[0];

// Access the collection of scenarios in the worksheet
XlsScenarioCollection scenarios = worksheet.Scenarios;

// Initialize list objects with different values for scenarios
List<object> currentChangePercentage_Values = new List<object> { 0.23, 0.8, 1.1, 0.5, 0.35, 0.2 };
List<object> increasedChangePercentage_Values = new List<object> { 0.45, 0.56, 0.9, 0.5, 0.58, 0.43 };
List<object> decreasedChangePercentage_Values = new List<object> { 0.3, 0.2, 0.5, 0.3, 0.5, 0.23 };
List<object> currentQuantity_Values = new List<object> { 1500, 3000, 5000, 4000, 500, 4000 };
List<object> increasedQuantity_Values = new List<object> { 1000, 5000, 4500, 3900, 10000, 8900 };
List<object> decreasedQuantity_Values = new List<object> { 1000, 2000, 3000, 3000, 300, 4000 };

// Add scenarios in the worksheet with different values for the same cells
scenarios.Add("Current % of Change", worksheet.Range["F5:F10"], currentChangePercentage_Values);
scenarios.Add("Increased % of Change", worksheet.Range["F5:F10"], increasedChangePercentage_Values);
scenarios.Add("Decreased % of Change", worksheet.Range["F5:F10"], decreasedChangePercentage_Values);
scenarios.Add("Current Quantity", worksheet.Range["D5:D10"], currentQuantity_Values);
scenarios.Add("Increased Quantity", worksheet.Range["D5:D10"], increasedQuantity_Values);
scenarios.Add("Decreased Quantity", worksheet.Range["D5:D10"], decreasedQuantity_Values);

// Saving the workbook
wb.SaveToFile(outputFile, ExcelVersion.Version2013);
wb.Dispose();

Generating scenario summary:

Workbook wb = new Workbook();
wb.LoadFromFile(inputFile);
Worksheet worksheet = wb.Worksheets[0];

// Access the collection of scenarios in the worksheet
XlsScenarioCollection scenarios = worksheet.Scenarios;

// Initialize list objects with different values for scenarios
List<object> currentChangePercentage_Values = new List<object>{ 0.23, 0.8, 1.1, 0.5, 0.35, 0.2 };
List<object> increasedChangePercentage_Values = new List<object> { 0.45, 0.56, 0.9, 0.5, 0.58, 0.43 };
List<object> decreasedChangePercentage_Values = new List<object> { 0.3, 0.2, 0.5, 0.3, 0.5, 0.23 };
List<object> currentQuantity_Values = new List<object> { 1500, 3000, 5000, 4000, 500, 4000 };
List<object> increasedQuantity_Values = new List<object> { 1000, 5000, 4500, 3900, 10000, 8900 };
List<object> decreasedQuantity_Values = new List<object> { 1000, 2000, 3000, 3000, 300, 4000 };

// Add scenarios in the worksheet with different values for the same cells
scenarios.Add("Current % of Change", worksheet.Range["F5:F10"], currentChangePercentage_Values);
scenarios.Add("Increased % of Change", worksheet.Range["F5:F10"], increasedChangePercentage_Values);
scenarios.Add("Decreased % of Change", worksheet.Range["F5:F10"], decreasedChangePercentage_Values);
scenarios.Add("Current Quantity", worksheet.Range["D5:D10"], currentQuantity_Values);
scenarios.Add("Increased Quantity", worksheet.Range["D5:D10"], increasedQuantity_Values);
scenarios.Add("Decreased Quantity", worksheet.Range["D5:D10"], decreasedQuantity_Values);

// Create Summary
worksheet.Scenarios.Summary(worksheet.Range["L7"]);

// Saving the workbook
wb.SaveToFile(outputFile, ExcelVersion.Version2013);
wb.Dispose();

Editing scenario:

Workbook wb = new Workbook();
wb.LoadFromFile(inputFile);
Worksheet worksheet = wb.Worksheets[0];

// Access the collection of scenarios in the worksheet
XlsScenarioCollection scenarios = worksheet.Scenarios;
XlsScenario scenario1 = scenarios[0];
XlsScenario scenario2 = scenarios[1];

// Modify the scenario 
scenario1.SetVariableCells(worksheet.Range["A1:A5"], scenario2.Values);

CellRange sourceCell = worksheet.Range["B1:B5"];
scenario2.SetVariableCells(sourceCell, scenario2.Values);

scenario1.Show();
scenario2.Show();

// Saving the workbook
wb.SaveToFile(outputFile, ExcelVersion.Version2013);
wb.Dispose();

Merging scenario:

Workbook wb = new Workbook();
wb.LoadFromFile(inputFile);
Worksheet worksheet1 = wb.Worksheets[0];
Worksheet worksheet2 = wb.Worksheets[1];

// Merge the scenario 
worksheet1.Scenarios.Merge(worksheet2);

// Saving the workbook
wb.SaveToFile(outputFile, ExcelVersion.Version2013);
wb.Dispose();

Deleting scenario:

Workbook wb = new Workbook();
wb.LoadFromFile(inputFile);
Worksheet worksheet = wb.Worksheets[0];

// Access the collection of scenarios in the worksheet
XlsScenarioCollection scenarios = worksheet.Scenarios;

// delete the scenario 
scenarios.RemoveScenarioAt(0);
scenarios.RemoveScenarioByName("two");

string content = "";
content += "Count:" + scenarios.Count + "\n";
content += "ContainsScenario:" + scenarios.ContainsScenario("two").ToString() + "\n";
content += "ContainsScenario:" + scenarios.ContainsScenario("one").ToString() + "\n";
File.WriteAllText(outputFile, content.ToString());

// Saving the workbook
wb.SaveToFile(outputFile, ExcelVersion.Version2013);
wb.Dispose();
Bug Fix SPIREXLS-5995 SPIREXLS-6077 Fixed the issue where macros were lost when copying worksheets.
Click the link to download Spire.XLS 16.3.6:
More information of Spire.XLS new release or hotfix: