VBA Projects
VBA Projects
This guide shows how to inspect and modify the VBA project embedded in a macro-enabled document with Aspose.Words FOSS for .NET. Aspose.Words FOSS for .NET can inspect and modify the VBA project embedded in a macro-enabled document (.docm) — its modules, source code, and library references — through Document.VbaProject and the VbaProject/VbaModule object model. This is project inspection and editing, not macro execution: Aspose.Words has never included a VBA runtime, in the commercial product or this FOSS edition, so VBA code is read and modified as data, not run.
Accessing a Document’s VBA Project
Document.VbaProject returns a VbaProject, or null if the document has no VBA project. Document.HasMacros reports whether a VBA project is present without you having to access VbaProject first. VbaProject.Name, CodePage, IsSigned, and IsProtected describe the project itself.
Reading and Editing Modules
VbaProject.Modules returns a VbaModuleCollection. Index into it by name to look up a specific module (e.g. modules["ModuleName"]), or enumerate the whole collection. Each VbaModule exposes its Name, Type (a VbaModuleType: DocumentModule, ProceduralModule, ClassModule, or DesignerModule), and — critically — its SourceCode as a writable string property, so module source can be read and rewritten programmatically. VbaModuleCollection.Add(vbaModule) and Remove(module) add or remove modules from the project.
Library References
VbaProject.References returns a VbaReferenceCollection of VbaReference objects — the Automation type libraries and other VBA projects the macro code depends on. VbaReference.Type reports a VbaReferenceType (Registered, Project, Original, or Control), and LibId identifies the referenced library. VbaReferenceCollection.Remove(item) / RemoveAt(index) remove references.
Removing a VBA Project
Document.RemoveMacros() strips the VBA project from a document entirely — useful for producing a macro-free copy of a document that originally shipped with macros.
Tips and Best Practices
- Check
Document.HasMacrosbefore touchingDocument.VbaProjectto avoid null-handling for documents that never had macros. - Treat
VbaModule.SourceCodeas plain text — since there’s no VBA runtime, editing it doesn’t validate the code; malformed VBA will still round-trip, but won’t run correctly in Word until corrected. - Use
VbaProject.IsSigned/IsProtectedto detect signed or protected projects before attempting to modify their modules. Document.RemoveMacros()is the simplest way to strip macros for a “clean” distribution copy, rather than manually clearing every module.
Common Issues
| Issue | Cause | Fix |
|---|---|---|
Document.VbaProject is null | The document has no VBA project | Check Document.HasMacros first before accessing VbaProject members |
Expecting macros to run after editing SourceCode | Aspose.Words has no VBA execution engine | Use this API to read/modify macro source and structure only; run the resulting document in an environment with a VBA runtime, such as Microsoft Word, to execute the code |
Indexing VbaModuleCollection by name returns nothing | Module name mismatch (case or exact spelling) | Enumerate VbaProject.Modules to confirm the exact module name |
FAQ
Can Aspose.Words FOSS for .NET run VBA macros in a document?
No. Neither this edition nor the commercial Aspose.Words product includes a VBA execution engine — VBA projects are read and modified as data (module source, references, project metadata), never executed.
How do I read a macro’s source code?
Get the module by indexing VbaProject.Modules by name (e.g. modules["ModuleName"]), then read its SourceCode property.
Can I add a brand-new VBA module to a document?
Yes — construct a VbaModule and add it to the project’s VbaModuleCollection via Add(vbaModule).
How do I check whether a document even has macros before processing it?
Read Document.HasMacros; it’s true if the document carries a VBA project, without you having to access Document.VbaProject first.
How do I strip all macros from a document?
Call Document.RemoveMacros().
API Reference Summary
| Class / Method | Description |
|---|---|
Document.VbaProject | The document’s VbaProject, or null if it has no macros |
Document.HasMacros | Whether a VBA project is present |
VbaProject | Name, Modules, References, CodePage, IsSigned, IsProtected |
VbaModule | One module: Name, Type (VbaModuleType), writable SourceCode |
VbaModuleCollection | Add(), indexer by name, Remove(), Count |
VbaReference / VbaReferenceCollection | Referenced type libraries: Type (VbaReferenceType), LibId |
Document.RemoveMacros() | Strips the VBA project from the document |