Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active April 3, 2023 09:58
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save aspose-com-gists/41f3b1d61a73dadd899491b6e0e6af2b to your computer and use it in GitHub Desktop.
Save aspose-com-gists/41f3b1d61a73dadd899491b6e0e6af2b to your computer and use it in GitHub Desktop.
Work with VBA Macros in PowerPoint PPT/PPTX in Python
# Create or load a presentation
with slides.Presentation() as presentation:
# Create new VBA project
presentation.vba_project = slides.vba.VbaProject()
# Add empty module to the VBA project
module = presentation.vba_project.modules.add_empty_module("Module")
# Set module source code
module.source_code = "Sub Test(oShape As Shape) MsgBox ""Test"" End Sub"
# Create reference to <stdole>
stdoleReference = slides.vba.VbaReferenceOleTypeLib("stdole", "*\\G{00020430-0000-0000-C000-000000000046}#2.0#0#C:\\Windows\\system32\\stdole2.tlb#OLE Automation")
# Create reference to Office
officeReference =slides.vba.VbaReferenceOleTypeLib("Office", "*\\G{2DF8D04C-5BFA-101B-BDE5-00AA0044DE52}#2.0#0#C:\\Program Files\\Common Files\\Microsoft Shared\\OFFICE14\\MSO.DLL#Microsoft Office 14.0 Object Library")
# add references to the VBA project
presentation.vba_project.references.add(stdoleReference)
presentation.vba_project.references.add(officeReference)
# Save presentation
presentation.save("add-vba-macro.pptm", slides.export.SaveFormat.PPTM)
# Load a presentation
with slides.Presentation("presentation.pptm") as presentation:
# Check if presentation contains VBA Project
if presentation.vba_project is not None:
# Print each module
for module in presentation.vba_project.modules:
print(module.name)
print(module.source_code)
# Load a presentation
with slides.Presentation("presentation.pptm") as presentation:
# Remove VBA macro using index
presentation.vba_project.modules.remove(presentation.vba_project.modules[0])
# Save presentation
presentation.save("remove-vba-macro.pptm", slides.export.SaveFormat.PPTM)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment