Learn how to create, modify or clone VBA macro in Word document using Python: https://blog.aspose.com/2021/11/23/work-with-vba-macro-in-word-using-python/
Last active
July 12, 2022 21:16
-
-
Save aspose-com-gists/2322b84d38b01cbc8f3b7f57bf2a2a7f to your computer and use it in GitHub Desktop.
Work with VBA Macro in Word Documents in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aspose.words as aw | |
# Load source Word document. | |
doc = aw.Document("document.docm") | |
# Load destination Word document. | |
destDoc = aw.Document("dest_document.docm") | |
destDoc.vba_project = aw.vba.VbaProject() | |
# Clone VBA module. | |
copyModule = doc.vba_project.modules.get_by_name("Module1").clone() | |
destDoc.vba_project.modules.add(copyModule) | |
# Save document. | |
destDoc.save("clone_vba_module.docm") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aspose.words as aw | |
# Load source Word document. | |
doc = aw.Document("document.docm") | |
# Load destination Word document. | |
destDoc = aw.Document("dest_document.docm") | |
# Clone VBA project. | |
destDoc.vba_project = doc.vba_project.clone() | |
# Save document. | |
destDoc.save("clone_vba_module.docm") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aspose.words as aw | |
# Load Word document. | |
doc = aw.Document("document.docm") | |
# Create VBA project | |
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) | |
# Save document. | |
doc.save("create_vba_project.docm") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import aspose.words as aw | |
# Load Word document. | |
doc = aw.Document("document.docm") | |
# Get reference of VBA project. | |
project = doc.vba_project | |
# Update source code. | |
newSourceCode = "Test change source code" | |
project.modules[0].source_code = newSourceCode | |
# Save document. | |
doc.save("modify_vba_macros.docm") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment