Working with Macros in PowerPoint Presentation

3 Aug 20263 minutes to read

A macro is a series of commands that can be grouped together as a single command to automate frequently used tasks. Macros can be created for Microsoft PowerPoint using Visual Basic for Applications (VBA). Refer to the Create a macro in PowerPoint documentation for more details.

Macro-enabled PowerPoint presentations of file types .PPTM and .POTM can be loaded, modified, and saved while preserving the embedded VBA project using the Syncfusion Presentation library.

Loading and saving a macro-enabled presentation

The following code example illustrates how to load a macro-enabled PowerPoint presentation and save it back to disk while preserving the macros.

//Open an existing macro-enabled PowerPoint presentation
IPresentation pptxDoc = Presentation.Open("Sample.PPTM");
//Add a blank slide to the presentation
ISlide slide = pptxDoc.Slides.Add(SlideLayoutType.Blank);
//Add a text box to the slide
IParagraph paragraph = slide.Shapes.AddTextBox(100, 100, 300, 80).TextBody.AddParagraph("Preserve Macros");
//Save the PowerPoint presentation
pptxDoc.Save("Output.PPTM");
//Close the presentation
pptxDoc.Close();
//Open an existing macro-enabled PowerPoint presentation
IPresentation pptxDoc = Presentation.Open("Sample.PPTM");
//Add a blank slide to the presentation
ISlide slide = pptxDoc.Slides.Add(SlideLayoutType.Blank);
//Add a text box to the slide
IParagraph paragraph = slide.Shapes.AddTextBox(100, 100, 300, 80).TextBody.AddParagraph("Preserve Macros");
//Save the presentation
pptxDoc.Save("Output.PPTM");
//Close the presentation
pptxDoc.Close();
'Open an existing macro-enabled PowerPoint presentation
Dim pptxDoc As IPresentation = Presentation.Open("Sample.PPTM")
'Add a blank slide to the presentation
Dim slide As ISlide = pptxDoc.Slides.Add(SlideLayoutType.Blank)
'Add a text box to the slide
Dim paragraph As IParagraph = slide.Shapes.AddTextBox(100, 100, 300, 80).TextBody.AddParagraph("Preserve Macros")
'Save the presentation
pptxDoc.Save("Output.PPTM")
'Close the presentation
pptxDoc.Close()

You can download a complete working sample from GitHub.

Removing macros from a macro-enabled presentation

The following code example illustrates how to remove the macros present in the presentation,

//Open an existing macro-enabled PowerPoint presentation
IPresentation pptxDoc = Presentation.Open("Sample.PPTM");
//Check whether the presentation has macros and then remove them
if (pptxDoc.HasMacros)
    pptxDoc.RemoveMacros();
//Save the PowerPoint presentation
pptxDoc.Save("Output.pptx");
//Close the presentation
pptxDoc.Close();
//Open an existing macro-enabled PowerPoint presentation
IPresentation pptxDoc = Presentation.Open("Sample.PPTM");
//Check whether the presentation has macros and then remove them
if (pptxDoc.HasMacros)
    pptxDoc.RemoveMacros();
//Save the presentation
pptxDoc.Save("Output.pptx");
//Close the presentation
pptxDoc.Close();
'Open an existing macro-enabled PowerPoint presentation
Dim pptxDoc As IPresentation = Presentation.Open("Sample.PPTM")
'Check whether the presentation has macros and then remove them
If pptxDoc.HasMacros Then
    pptxDoc.RemoveMacros()
End If
'Save the presentation
pptxDoc.Save("Output.pptx")
'Close the presentation
pptxDoc.Close()

You can download a complete working sample from GitHub.

See also