Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active April 3, 2023 09:58
Show Gist options
  • Select an option

  • Save aspose-com-gists/41f3b1d61a73dadd899491b6e0e6af2b to your computer and use it in GitHub Desktop.

Select an option

Save aspose-com-gists/41f3b1d61a73dadd899491b6e0e6af2b to your computer and use it in GitHub Desktop.

Revisions

  1. aspose-com-gists revised this gist Mar 7, 2022. 2 changed files with 2 additions and 2 deletions.
    2 changes: 1 addition & 1 deletion extract-vba-macro-in-powerpoint.py
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    # Load a presentation
    with slides.Presentation("presentation.pptm") as presentation:

    # check if presentation contains VBA Project
    # Check if presentation contains VBA Project
    if presentation.vba_project is not None:

    # Print each module
    2 changes: 1 addition & 1 deletion readme.md
    Original file line number Diff line number Diff line change
    @@ -1 +1 @@
    Read the complete article on how to add, extract, and remove VBA macros in PowerPoint using Python:
    Read the complete article on how to add, extract, and remove VBA macros in PowerPoint using Python: https://blog.aspose.com/2022/02/21/work-with-vba-macros-in-powerpoint-in-python/
  2. aspose-com-gists created this gist Mar 7, 2022.
    23 changes: 23 additions & 0 deletions add-vba-macro-in-powerpoint.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,23 @@
    # 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)
    10 changes: 10 additions & 0 deletions extract-vba-macro-in-powerpoint.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    # 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)
    1 change: 1 addition & 0 deletions readme.md
    Original file line number Diff line number Diff line change
    @@ -0,0 +1 @@
    Read the complete article on how to add, extract, and remove VBA macros in PowerPoint using Python:
    7 changes: 7 additions & 0 deletions remove-vba-macro-in-powerpoint.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    # 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)