Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
126 commits
Select commit Hold shift + click to select a range
2026a2b
Add example: load-an-xlsm-workbook-from-disk-and-obtain-its-vbaprojec…
agent-aspose-cells-examples May 21, 2026
daf5fe7
Add example: if-the-project-is-unprotected-apply-password-protection-…
agent-aspose-cells-examples May 21, 2026
72c3d4d
Add example: create-a-new-vba-module-named-automation-within-the-vbap…
agent-aspose-cells-examples May 21, 2026
049c332
Add example: insert-a-multiline-vba-subroutine-into-the-automation-mo…
agent-aspose-cells-examples May 21, 2026
a4618e4
Add example: save-the-modified-workbook-as-an-xlsm-file-to-preserve-t…
agent-aspose-cells-examples May 21, 2026
98733ea
Add example: load-a-macroenabled-workbook-from-a-memory-stream-and-ve…
agent-aspose-cells-examples May 21, 2026
c9ca547
Add example: enumerate-all-modules-in-the-vbaproject-and-output-each-…
agent-aspose-cells-examples May 21, 2026
62b01b7
Add example: remove-a-specified-module-using-modulesremoveat-and-save…
agent-aspose-cells-examples May 21, 2026
e78a55f
Add example: rename-an-existing-vba-module-to-dataprocessor-by-settin…
agent-aspose-cells-examples May 21, 2026
3a45f16
Add example: export-the-vbaprojects-digital-certificate-to-a-file-str…
agent-aspose-cells-examples May 21, 2026
d449ea6
Add example: protect-all-xlsm-files-in-a-directory-applying-passwords…
agent-aspose-cells-examples May 21, 2026
f179f51
Add example: validate-that-each-added-module-contains-a-sub-main-entr…
agent-aspose-cells-examples May 21, 2026
32177d0
Add example: serialize-the-vba-project-structure-including-module-nam…
agent-aspose-cells-examples May 21, 2026
212d867
Add example: clear-existing-code-from-a-specific-module-and-insert-up…
agent-aspose-cells-examples May 21, 2026
bcdc7d0
Add example: log-a-warning-if-the-vba-project-is-locked-for-viewing-a…
agent-aspose-cells-examples May 21, 2026
ec7c820
Add example: clone-a-workbook-duplicate-its-vba-project-and-save-the-…
agent-aspose-cells-examples May 21, 2026
31a418e
Add example: skip-protecting-workbooks-that-are-already-secured-by-us…
agent-aspose-cells-examples May 21, 2026
891dca2
Add example: implement-error-handling-around-vbaprojectprotect-to-cap…
agent-aspose-cells-examples May 21, 2026
45c9610
Add example: export-each-workbooks-vba-module-code-to-separate-bas-fi…
agent-aspose-cells-examples May 21, 2026
e3edcf4
Add example: load-a-workbook-from-a-network-share-protect-its-vba-pro…
agent-aspose-cells-examples May 21, 2026
193aeed
Add example: remove-any-module-named-temp-from-a-collection-of-workbo…
agent-aspose-cells-examples May 21, 2026
8bde161
Add example: create-a-new-vba-module-with-utf8-code-page-and-add-mult…
agent-aspose-cells-examples May 21, 2026
17c6b0e
Add example: attempt-to-unlock-a-vba-project-locked-for-viewing-using…
agent-aspose-cells-examples May 21, 2026
eda145a
Add example: generate-a-summary-of-all-vba-modules-including-line-cou…
agent-aspose-cells-examples May 21, 2026
1af09cf
Add example: apply-password-protection-only-when-the-workbook-contain…
agent-aspose-cells-examples May 21, 2026
a994acb
Add example: delete-any-vba-module-that-exceeds-five-hundred-lines-of…
agent-aspose-cells-examples May 21, 2026
6a2ee15
Add example: copy-a-vba-module-from-one-workbook-to-another-preservin…
agent-aspose-cells-examples May 21, 2026
7db09ff
Add example: add-a-module-that-references-external-com-libraries-and-…
agent-aspose-cells-examples May 21, 2026
4630c3a
Add example: create-a-macro-that-iterates-through-all-worksheets-and-…
agent-aspose-cells-examples May 21, 2026
fb80673
Add example: validate-that-the-vba-project-password-meets-minimum-len…
agent-aspose-cells-examples May 21, 2026
deb6db3
Add example: import-an-exported-vba-project-certificate-into-another-…
agent-aspose-cells-examples May 21, 2026
a1fc7f0
Add example: create-a-new-xlsm-workbook-instance-and-add-a-vba-code-m…
agent-aspose-cells-examples May 21, 2026
a1bf748
Add example: insert-a-form-control-button-onto-a-specific-worksheet-cell
agent-aspose-cells-examples May 21, 2026
310e7c4
Add example: set-the-buttons-macroname-property-to-reference-the-newl…
agent-aspose-cells-examples May 21, 2026
79e89e7
Add example: save-the-workbook-as-a-macroenabled-xlsm-file-to-the-spe…
agent-aspose-cells-examples May 21, 2026
4bab479
Add example: load-an-existing-xlsm-workbook-from-a-file-path-into-a-w…
agent-aspose-cells-examples May 21, 2026
bf036ec
Add example: retrieve-the-vba-project-from-the-loaded-workbook-via-wo…
agent-aspose-cells-examples May 21, 2026
06d9d56
Add example: verify-the-vba-projects-digital-signature-status-using-t…
agent-aspose-cells-examples May 21, 2026
9680e06
Add example: add-a-registered-library-reference-to-the-vba-project-us…
agent-aspose-cells-examples May 21, 2026
2b65151
Add example: export-the-vba-projects-digital-certificate-to-a-file-path
agent-aspose-cells-examples May 21, 2026
0cd7247
Add example: export-the-vba-projects-digital-certificate-to-a-memorys…
agent-aspose-cells-examples May 21, 2026
dd37b86
Add example: confirm-that-the-exported-certificate-file-size-matches-…
agent-aspose-cells-examples May 21, 2026
a563df3
Add example: validate-that-the-exported-certificate-stream-length-mat…
agent-aspose-cells-examples May 21, 2026
4192a04
Add example: programmatically-check-each-workbook-in-a-collection-for…
agent-aspose-cells-examples May 21, 2026
502d4f4
Add example: use-a-trycatch-block-to-handle-exceptions-when-exporting…
agent-aspose-cells-examples May 21, 2026
459a0dd
Add example: batch-export-certificates-from-all-signed-workbooks-in-a…
agent-aspose-cells-examples May 21, 2026
94db050
Add example: compare-exported-certificate-files-with-original-certifi…
agent-aspose-cells-examples May 21, 2026
b76013d
Add example: add-a-custom-reference-to-a-vba-project-that-points-to-a…
agent-aspose-cells-examples May 21, 2026
ddb29a2
Add example: programmatically-rename-a-form-control-button-while-pres…
agent-aspose-cells-examples May 21, 2026
8c7a250
Add example: check-the-issigned-property-after-adding-a-digital-signa…
agent-aspose-cells-examples May 21, 2026
5e36b26
Add example: create-a-unit-test-that-verifies-macro-assignment-fails-…
agent-aspose-cells-examples May 21, 2026
a8f82fe
Add example: implement-a-method-that-copies-a-macro-from-one-workbook…
agent-aspose-cells-examples May 21, 2026
b91574d
Add example: assign-different-macros-to-multiple-form-controls-on-the…
agent-aspose-cells-examples May 21, 2026
69c3c03
Add example: generate-a-csv-file-containing-workbook-names-macro-assi…
agent-aspose-cells-examples May 21, 2026
9ec135d
Add example: validate-that-adding-a-library-reference-throws-an-excep…
agent-aspose-cells-examples May 21, 2026
f6c18a0
Add example: export-a-vba-certificate-to-a-temporary-file-then-load-i…
agent-aspose-cells-examples May 21, 2026
354b584
Add example: detect-and-report-any-duplicate-macro-names-across-multi…
agent-aspose-cells-examples May 21, 2026
457c0be
Add example: log-detailed-information-about-each-macro-assignment-inc…
agent-aspose-cells-examples May 21, 2026
ea64ab2
Add example: implement-a-function-that-checks-whether-a-workbook-cont…
agent-aspose-cells-examples May 21, 2026
1fd9a51
Add example: batch-process-workbooks-to-add-a-standard-library-refere…
agent-aspose-cells-examples May 21, 2026
ae96051
Add example: use-reflection-to-enumerate-all-vba-project-references-a…
agent-aspose-cells-examples May 21, 2026
27ad4d4
Add example: export-certificates-from-workbooks-using-asynchronous-ta…
agent-aspose-cells-examples May 21, 2026
c4d1313
Add example: develop-a-console-application-that-accepts-a-folder-path…
agent-aspose-cells-examples May 21, 2026
8071091
Add example: load-an-excel-workbook-and-read-the-issigned-property-to…
agent-aspose-cells-examples May 21, 2026
b0bea2c
Add example: load-workbook-with-loadoptionsfiltervbaproject-to-exclud…
agent-aspose-cells-examples May 21, 2026
3bb4688
Add example: validate-the-vba-projects-digital-signature-using-workbo…
agent-aspose-cells-examples May 21, 2026
ebeeae1
Add example: sign-the-vba-project-with-a-certificate-loaded-from-a-pf…
agent-aspose-cells-examples May 21, 2026
8d1a49d
Add example: sign-the-vba-project-using-a-certificate-retrieved-from-…
agent-aspose-cells-examples May 21, 2026
5bf202a
Add example: export-the-vba-projects-certificate-to-a-file-path-and-v…
agent-aspose-cells-examples May 21, 2026
b2a09da
Add example: export-the-vba-projects-certificate-to-a-memorystream-an…
agent-aspose-cells-examples May 21, 2026
c2daade
Add example: save-the-signed-workbook-to-a-new-location-ensuring-the-…
agent-aspose-cells-examples May 21, 2026
f6b0dc5
Add example: log-the-signing-result-to-console-output-indicating-succ…
agent-aspose-cells-examples May 21, 2026
0c7be4e
Add example: batch-process-a-folder-of-excel-files-signing-each-vba-p…
agent-aspose-cells-examples May 21, 2026
c6f88f2
Add example: batch-validate-signatures-of-multiple-workbooks-and-gene…
agent-aspose-cells-examples May 21, 2026
416f435
Add example: detect-unsigned-vba-projects-across-a-directory-and-list…
agent-aspose-cells-examples May 21, 2026
94768f9
Add example: copy-userform-designerstorage-from-a-template-workbook-t…
agent-aspose-cells-examples May 21, 2026
8c0b406
Add example: preserve-existing-macros-while-copying-userform-storage-…
agent-aspose-cells-examples May 21, 2026
fe6a6fe
Add example: load-workbook-using-loadoptions-to-omit-vba-project-and-…
agent-aspose-cells-examples May 21, 2026
100a702
Add example: verify-that-vba-project-signature-becomes-invalid-after-…
agent-aspose-cells-examples May 21, 2026
c0bc271
Add example: resign-vba-project-after-code-changes-to-restore-a-valid…
agent-aspose-cells-examples May 21, 2026
19f4f4b
Add example: create-x509certificate2-from-pfx-file-with-password-and-…
agent-aspose-cells-examples May 21, 2026
c251073
Add example: sign-workbook-using-certificate-from-windows-store-selec…
agent-aspose-cells-examples May 21, 2026
8369b2b
Add example: export-certificate-to-byte-array-then-write-bytes-to-fil…
agent-aspose-cells-examples May 21, 2026
682f64d
Add example: save-signed-workbook-under-new-filename-to-preserve-orig…
agent-aspose-cells-examples May 21, 2026
f6c9ec4
Add example: overwrite-original-workbook-after-successful-signing-whe…
agent-aspose-cells-examples May 21, 2026
6fa835e
Add example: wrap-signing-operation-in-trycatch-block-to-handle-excep…
agent-aspose-cells-examples May 21, 2026
ca2ef81
Add example: write-validation-errors-to-a-text-file-for-later-analysi…
agent-aspose-cells-examples May 21, 2026
e58646b
Add example: compare-exported-certificate-thumbprint-with-original-ce…
agent-aspose-cells-examples May 21, 2026
881dc2b
Add example: load-workbook-from-memory-stream-sign-vba-project-then-w…
agent-aspose-cells-examples May 21, 2026
ea38e91
Add example: save-signed-workbook-to-network-location-via-stream-to-e…
agent-aspose-cells-examples May 21, 2026
8a10ca9
Add example: create-new-workbook-add-vba-module-with-code-then-digita…
agent-aspose-cells-examples May 21, 2026
fef3fed
Add example: remove-digital-signature-from-vba-project-by-clearing-ce…
agent-aspose-cells-examples May 21, 2026
a7a8f2e
Add example: load-workbook-with-loadoptionsfiltervbaproject-to-includ…
agent-aspose-cells-examples May 21, 2026
e9ed73f
Add example: generate-report-listing-signed-versus-unsigned-workbooks…
agent-aspose-cells-examples May 21, 2026
72ffc2e
Add example: automate-signing-of-excel-files-in-continuous-integratio…
agent-aspose-cells-examples May 21, 2026
3511322
Add example: use-asynchronous-tasks-to-sign-multiple-workbooks-concur…
agent-aspose-cells-examples May 21, 2026
a9b910b
Add example: load-a-workbook-using-loaddatafilteroptionsvba-to-retrie…
agent-aspose-cells-examples May 21, 2026
c029fd7
Add example: open-a-macro-enabled-workbook-while-skipping-worksheet-c…
agent-aspose-cells-examples May 21, 2026
a77a534
Add example: verify-whether-the-vba-project-in-the-loaded-workbook-is…
agent-aspose-cells-examples May 21, 2026
40df1c7
Add example: check-if-the-vba-project-is-locked-for-viewing-before-ex…
agent-aspose-cells-examples May 21, 2026
66e1e0a
Add example: retrieve-the-list-of-com-library-references-from-the-vba…
agent-aspose-cells-examples May 21, 2026
6714971
Add example: add-a-com-library-reference-eg-microsoft-scripting-runti…
agent-aspose-cells-examples May 21, 2026
207f28a
Add example: add-a-reference-to-the-microsoft-outlook-object-library-…
agent-aspose-cells-examples May 21, 2026
b0c2848
Add example: add-a-reference-to-the-microsoft-xml-v60-library-in-the-…
agent-aspose-cells-examples May 21, 2026
c8aaf8f
Add example: export-the-vba-projects-digital-certificate-to-a-pem-fil…
agent-aspose-cells-examples May 21, 2026
3dc1329
Add example: export-the-vba-projects-certificate-to-a-cer-file-and-ve…
agent-aspose-cells-examples May 21, 2026
3de9ae9
Add example: export-the-vba-projects-certificate-to-a-base64-string-f…
agent-aspose-cells-examples May 21, 2026
6d3e649
Add example: export-the-vba-projects-certificate-to-a-pfx-file-protec…
agent-aspose-cells-examples May 21, 2026
5c4e901
Add example: sign-the-vba-project-using-a-pfx-certificate-file-protec…
agent-aspose-cells-examples May 21, 2026
724c97e
Add example: sign-the-vba-project-with-a-certificate-stored-in-the-wi…
agent-aspose-cells-examples May 21, 2026
649a621
Add example: validate-the-digital-signature-of-the-vba-project-and-re…
agent-aspose-cells-examples May 21, 2026
ed81cfb
Add example: validate-the-vba-projects-signature-against-a-trusted-ro…
agent-aspose-cells-examples May 21, 2026
0ea422c
Add example: validate-the-digital-signature-of-a-vba-project-after-co…
agent-aspose-cells-examples May 21, 2026
8cd30cc
Add example: validate-the-digital-signature-of-a-vba-project-after-ad…
agent-aspose-cells-examples May 21, 2026
900263e
Add example: copy-all-macros-from-a-source-workbook-to-a-destination-…
agent-aspose-cells-examples May 21, 2026
c0fa339
Add example: copy-a-macro-that-generates-charts-from-a-template-workb…
agent-aspose-cells-examples May 21, 2026
766cf34
Add example: transfer-a-userform-called-inputform-between-two-workboo…
agent-aspose-cells-examples May 21, 2026
c641c4e
Add example: assign-a-macro-that-calculates-summary-statistics-to-a-s…
agent-aspose-cells-examples May 21, 2026
873f450
Add example: embed-vba-code-that-disables-the-print-command-into-a-wo…
agent-aspose-cells-examples May 21, 2026
d328102
Add example: load-a-workbook-with-vba-data-filtered-then-enumerate-al…
agent-aspose-cells-examples May 21, 2026
371d9e4
Add example: load-a-workbook-with-vba-data-filtered-then-list-all-for…
agent-aspose-cells-examples May 21, 2026
4db6592
Add example: load-a-workbook-with-vba-filter-then-extract-the-vba-pro…
agent-aspose-cells-examples May 21, 2026
f3d3690
Add example: load-a-workbook-with-vba-filter-then-extract-the-vba-pro…
agent-aspose-cells-examples May 21, 2026
0a7c3f9
Add example: batch-process-a-folder-of-macro-enabled-workbooks-adding…
agent-aspose-cells-examples May 21, 2026
349517c
Update agents.md
agent-aspose-cells-examples May 31, 2026
7ec14a8
Resolve index.json merge conflicts with release/26.5.0
agent-aspose-cells-examples May 31, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
using System;
using Aspose.Cells;
using Aspose.Cells.Vba;

namespace AsposeCellsVbaReferenceDemo
{
class Program
{
static void Main()
{
// Create a new workbook (macro-enabled format will be used on save)
Workbook workbook = new Workbook();

// Access the VBA project associated with the workbook
VbaProject vbaProject = workbook.VbaProject;

// Define the LibID for the Microsoft Scripting Runtime (scrrun.dll)
// The format is: *\G{<GUID>}#<Version>#0#<FullPath>#<Description>
string scriptingLibId = "*\\G{420B2830-E718-11CF-893D-00A0C9054228}#1.0#0#C:\\Windows\\System32\\scrrun.dll#Microsoft Scripting Runtime";

// Add the COM library reference to the VBA project
vbaProject.References.AddRegisteredReference("Scripting", scriptingLibId);

// Save the workbook as a macro-enabled file (XLSM)
workbook.Save("WorkbookWithScriptingReference.xlsm", SaveFormat.Xlsm);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using System;
using Aspose.Cells;
using Aspose.Cells.Vba;

class AddComReference
{
static void Main()
{
// Create a new workbook (macro-enabled)
Workbook workbook = new Workbook();

// Access the VBA project associated with the workbook
VbaProject vbaProject = workbook.VbaProject;

// Define the COM library reference (Automation type library)
// Example: Microsoft Scripting Runtime (scrrun.dll)
string referenceName = "Scripting";
string libid = "*\\G{420B2830-E718-11CF-893D-00A0C9054228}#1.0#0#C:\\Windows\\System32\\scrrun.dll#Scripting Runtime";

// Add the reference to the VBA project
vbaProject.References.AddRegisteredReference(referenceName, libid);

// Save the workbook as a macro-enabled file
workbook.Save("WorkbookWithComReference.xlsm", SaveFormat.Xlsm);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
using System;
using Aspose.Cells;
using Aspose.Cells.Vba;

namespace AsposeCellsComReferenceDemo
{
public class Program
{
public static void Main()
{
// Create a new workbook (macro-enabled format will be used on save)
Workbook workbook = new Workbook();

// Access the VBA project associated with the workbook
VbaProject vbaProject = workbook.VbaProject;

// ------------------------------------------------------------
// Add a reference to an external COM (Automation) library.
// Here we reference the standard OLE Automation library (stdole).
// ------------------------------------------------------------
vbaProject.References.AddRegisteredReference(
"stdole",
"*\\G{00020430-0000-0000-C000-000000000046}#2.0#0#C:\\Windows\\system32\\stdole2.tlb#OLE Automation");

// ------------------------------------------------------------
// Optionally, add a control reference (e.g., Microsoft Forms 2.0).
// This demonstrates adding a twiddled type library reference.
// ------------------------------------------------------------
vbaProject.References.AddControlRefrernce(
"MSForms",
"*\\G{0D452EE1-E08F-101A-852E-02608C4D0BB4}#2.0#0#C:\\Windows\\system32\\FM20.DLL#Microsoft Forms 2.0 Object Library",
"twiddledLibid_placeholder",
"extendedLibid_placeholder");

// ------------------------------------------------------------
// Add a new VBA module to the project.
// The module type is Class, and we give it a meaningful name.
// ------------------------------------------------------------
int moduleIndex = vbaProject.Modules.Add(VbaModuleType.Class, "ComHelperModule");
VbaModule module = vbaProject.Modules[moduleIndex];

// Insert VBA code that utilizes the referenced COM library.
// Example: use the stdole library to create a picture object.
module.Codes = @"
Public Sub ShowStdOleMessage()
Dim pic As stdole.StdPicture
MsgBox ""COM library reference is working!""
End Sub
";

// Save the workbook as a macro-enabled file so that the VBA project is retained.
workbook.Save("ComReferenceDemo.xlsm", SaveFormat.Xlsm);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
using System;
using System.Text;
using Aspose.Cells;
using Aspose.Cells.Vba;

namespace AsposeCellsVbaOutlookReference
{
class Program
{
static void Main()
{
// Create a new workbook (macro-enabled format will be used when saving)
Workbook workbook = new Workbook();

// Access the VBA project (automatically created for .xlsm files)
VbaProject vbaProject = workbook.VbaProject;

// Optional: set project name and encoding
vbaProject.Name = "OutlookAutomationProject";
vbaProject.Encoding = Encoding.UTF8;

// Add a reference to the Microsoft Outlook Object Library
// The libid string below is an example for Outlook 16.0; adjust the path/version as needed.
string outlookLibId = "*\\G{00062FFF-0000-0000-C000-000000000046}#9.0#0#C:\\Program Files\\Microsoft Office\\Office16\\MSOUTL.OLB#Microsoft Outlook 16.0 Object Library";
vbaProject.References.AddRegisteredReference("Outlook", outlookLibId);

// (Optional) Add a simple VBA module that uses Outlook – just for demonstration
int moduleIndex = vbaProject.Modules.Add(VbaModuleType.Class, "EmailHelper");
vbaProject.Modules[moduleIndex].Codes =
"Sub SendMail()\r\n" +
" Dim olApp As Outlook.Application\r\n" +
" Dim olMail As Outlook.MailItem\r\n" +
" Set olApp = New Outlook.Application\r\n" +
" Set olMail = olApp.CreateItem(0)\r\n" +
" olMail.Subject = \"Test Email\"\r\n" +
" olMail.Body = \"This email was sent from VBA in an Excel workbook.\"\r\n" +
" olMail.Recipients.Add \"example@example.com\"\r\n" +
" olMail.Send\r\n" +
"End Sub";

// Save the workbook as a macro-enabled file
workbook.Save("WorkbookWithOutlookReference.xlsm", SaveFormat.Xlsm);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
using System;
using Aspose.Cells;
using Aspose.Cells.Vba;

class AddMsXmlReference
{
static void Main()
{
// Create a new workbook
Workbook workbook = new Workbook();

// Save as a macro‑enabled workbook to ensure a VBA project is created,
// then reload it so the VbaProject property is available.
string tempPath = "temp.xlsm";
workbook.Save(tempPath, SaveFormat.Xlsm);
workbook = new Workbook(tempPath);

// Access the VBA project
VbaProject vbaProject = workbook.VbaProject;

// Add a reference to Microsoft XML, v6.0 (MSXML6) library
vbaProject.References.AddRegisteredReference(
"MSXML2",
"*\\G{88D969C0-F192-11D4-A65F-0040963251E5}#6.0#0#C:\\Windows\\System32\\msxml6.dll#Microsoft XML, v6.0");

// Save the workbook with the added reference
workbook.Save("WorkbookWithMsXmlReference.xlsm", SaveFormat.Xlsm);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
using System;
using System.IO;
using Aspose.Cells;
using Aspose.Cells.Vba;

namespace AsposeCellsVbaReferenceDemo
{
public class AddRegisteredReferenceDemo
{
public static void Run()
{
try
{
// Create a new workbook (will become macro-enabled when saved as .xlsm)
Workbook workbook = new Workbook();

// Access the VBA project associated with the workbook
VbaProject vbaProject = workbook.VbaProject;

// Add a registered reference to an Automation type library (example: stdole)
vbaProject.References.AddRegisteredReference(
"stdole",
"*\\G{00020430-0000-0000-C000-000000000046}#2.0#0#C:\\Windows\\system32\\stdole2.tlb#OLE Automation");

// Define output file path
string outputPath = "output.xlsm";

// Save the workbook as a macro-enabled file
workbook.Save(outputPath);
Console.WriteLine($"Workbook saved successfully to '{Path.GetFullPath(outputPath)}'.");
}
catch (Exception ex)
{
Console.Error.WriteLine($"Error: {ex.Message}");
}
}

// Entry point for the application
public static void Main(string[] args)
{
Run();
}
}
}
116 changes: 116 additions & 0 deletions macro-project/agents.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
---
category: macro-project
framework: .NET
parent: ../agents.md
version: v2
---

# Persona

You are a C# developer specializing in VBA projects, macros, and workbook automation using Aspose.Cells for .NET.

Generate simple, correct, production-quality examples that demonstrate ONE macro-project scenario at a time.

---

# Scope

- Standalone .cs examples
- One operation per example
- Fully runnable with dotnet run
- No external dependencies

---

# Required Namespaces

using System;
using Aspose.Cells;
using Aspose.Cells.Vba;

---

# Key APIs

- VbaProject
- VbaModule
- Workbook.VbaProject
- VbaProject.Modules
- VbaModuleCollection

---

# Common Pattern

1. Create workbook
2. Access or create VBA project
3. Add, read, or modify VBA modules
4. Verify project structure
5. Save workbook
6. Print success message

---

# Macro Project Rules

- Demonstrate one VBA feature per example
- Use meaningful module names
- Keep VBA code samples small and readable
- Focus on project management rather than complex VBA logic

---

# Input Strategy

- Do NOT rely on external XLSM files
- Create workbook and VBA content programmatically
- Keep examples self-contained

---

# Output Rules

- Always generate output.xlsm when macros are involved
- Ensure workbook is saved successfully
- Output files are written to the working directory

---

# Common Tasks

- Create VBA project
- Add VBA module
- Read VBA modules
- Modify VBA code
- Access VBA project information
- Save macro-enabled workbook

---

# Common Mistakes

❌ var workbook = new Workbook();
✅ Workbook workbook = new Workbook();

❌ Save macro workbook as output.xlsx
✅ Save macro-enabled workbooks as output.xlsm

❌ Workbook workbook = new Workbook("input.xlsm");
✅ Workbook workbook = new Workbook();

---

# Code Simplicity

- Keep examples concise
- Avoid unnecessary abstractions
- Focus on one VBA capability per example

---

# General Rules

Refer to the root agents.md for:
- Boundaries
- Testing requirements
- Build and run instructions
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
using System;
using Aspose.Cells;

namespace AsposeCellsPasswordPolicy
{
class Program
{
static void Main()
{
// Create a new workbook
Workbook workbook = new Workbook();

// Add worksheets to demonstrate the condition (total 12 worksheets)
for (int i = 0; i < 12; i++)
{
// The first worksheet already exists; add new ones for the rest
if (i > 0)
workbook.Worksheets.Add();
// Optionally put some data
workbook.Worksheets[i].Cells["A1"].PutValue($"Sheet {i + 1}");
}

// Apply password protection only if the workbook has more than ten worksheets
if (workbook.Worksheets.Count > 10)
{
// Protect the workbook structure with a password
workbook.Protect(ProtectionType.Structure, "StrongPassword!123");
}

// Save the workbook
workbook.Save("ProtectedIfMoreThanTenSheets.xlsx", SaveFormat.Xlsx);

// Clean up
workbook.Dispose();
}
}
}
Loading