Working with VBA Macros
Visual Basic for Applications (VBA) for Microsoft Word is a simple but powerful programming language that can be used to extend the functionality. Aspose.Words API provides three classes to get access to the VBA project source code:
- The VBAProject class provides access to VBA project information
- The VBAModuleCollection class returns the collection of VBA project modules
- The VbaModule class provides access to the VBA project module
- The VbaModuleType enumeration defines the types of a model in a VBA project. The module can be a procedural module, document module, class module, or designer module
Create a VBA Project
Aspose.Words API provides the vba_project property to get or set VbaProject in the document.
The following code example demonstrates how to create a VBA project and VBA Module along with basic properties e.g. name and type:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document() | |
project = aw.vba.VbaProject() | |
project.name = "AsposeProject" | |
doc.vba_project = project | |
# Create a new module and specify a macro source code. | |
module = aw.vba.VbaModule() | |
module.name = "AsposeModule" | |
module.type = aw.vba.VbaModuleType.PROCEDURAL_MODULE | |
module.source_code = "New source code" | |
# Add module to the VBA project. | |
doc.vba_project.modules.add(module) | |
doc.save(docs_base.artifacts_dir + "WorkingWithVba.create_vba_project.docm") |
Read Macros
Aspose.Words also provides users with the ability to read VBA macros.
The following code example shows how to read VBA Macros from the document:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "VBA project.docm") | |
if (doc.vba_project != None) : | |
for module in doc.vba_project.modules : | |
print(module.source_code) | |
Write or Modify Macros
Using Aspose.Words, users can modify VBA macros.
The following code example shows how to modify VBA Macros using the source_code property:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "VBA project.docm") | |
project = doc.vba_project | |
newSourceCode = "Test change source code" | |
project.modules[0].source_code = newSourceCode | |
doc.save(docs_base.artifacts_dir + "WorkingWithVba.modify_vba_macros.docm") |
Clone VBA Project
With Aspose.Words it is also possible to clone VBA projects.
The following code example shows how to clone the VBA Project using the clone property which creates a copy of the existing project:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "VBA project.docm") | |
destDoc = aw.Document() | |
destDoc.vba_project = doc.vba_project.clone() | |
destDoc.save(docs_base.artifacts_dir + "WorkingWithVba.clone_vba_project.docm") |
Clone VBA Module
You can also clone VBA modules if needed.
The following code example shows how to clone the VBA Module using the clone property which creates a copy of the existing project:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "VBA project.docm") | |
destDoc = aw.Document() | |
destDoc.vba_project = aw.vba.VbaProject() | |
copyModule = doc.vba_project.modules.get_by_name("Module1").clone() | |
destDoc.vba_project.modules.add(copyModule) | |
destDoc.save(docs_base.artifacts_dir + "WorkingWithVba.clone_vba_module.docm") |
Work with the VBA Project References
Aspose.Words API provides VbaReferenceCollection class to work with VBA Project References representing a collection of VBA project references.
The following code example shows how to remove some references from the collection of references from a VBA project:
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
doc = aw.Document(docs_base.my_dir + "VBA project.docm") | |
# Find and remove the reference with some LibId path. | |
brokenPath = "brokenPath.dll" | |
references = doc.vba_project.references | |
for i in range(references.count - 1, 0) : | |
reference = doc.vba_project.references.element_at(i) | |
path = get_lib_id_path(reference) | |
if (path == brokenPath) : | |
references.remove_at(i) | |
doc.save(docs_base.artifacts_dir + "WorkingWithVba.remove_broken_ref.docm") |
# For complete examples and data files, please go to https://github.com/aspose-words/Aspose.Words-for-Python-via-.NET | |
# <summary> | |
# Returns string representing LibId path of a specified reference. | |
# </summary> | |
def get_lib_id_path(self, reference) : | |
if reference.type == aw.vba.VbaReferenceType.REGISTERED or reference.type == aw.vba.VbaReferenceType.ORIGINAL or reference.type == aw.vba.VbaReferenceType.CONTROL : | |
return self.get_lib_id_reference_path(reference.lib_id) | |
elif reference.type == aw.vba.VbaReferenceType.PROJECT : | |
return self.get_lib_id_project_path(reference.lib_id) | |
else : | |
raise RuntimeError() | |
# <summary> | |
# Returns path from a specified identifier of an Automation type library. | |
# </summary> | |
# <remarks> | |
# Please see details for the syntax at [MS-OVBA], 2.1.1.8 LibidReference. | |
# </remarks> | |
@staticmethod | |
def get_lib_id_reference_path(libIdReference : str) : | |
if (libIdReference != None) : | |
refParts = libIdReference.split('#') | |
if (refParts.length > 3) : | |
return refParts[3] | |
return "" | |
# <summary> | |
# Returns path from a specified identifier of an Automation type library. | |
# </summary> | |
# <remarks> | |
# Please see details for the syntax at [MS-OVBA], 2.1.1.12 ProjectReference. | |
# </remarks> | |
@staticmethod | |
def get_lib_id_project_path(libIdProject : str) : | |
if (libIdProject != None) : | |
return libIdProject.substring(3) | |
return "" | |