Add VBA Macros and Ribbon Commands to PowerPoint Files from Excel
Question details
Add a VBA module and macro to multiple PowerPoint presentations from Excel, protect each VBA project, and provide a Ribbon command with a custom icon.
- Product
- Microsoft Excel and Microsoft PowerPoint
- Device & OS
- not provided
- Scenario
- An Excel VBA project prepares multiple PowerPoint files with a macro and a convenient way to run it.
- Observed behavior
- The user needs a macro-enabled PowerPoint file with a protected VBA project and a custom Ribbon command.
Use desktop versions of Excel and PowerPoint that support VBA, and enable trusted access to the VBA project object model in PowerPoint's Trust Center. Prepare a separate output folder for the macro-enabled presentations and use only code and files you trust.
Add the macro from Excel and customize each PowerPoint Ribbon
Use Excel VBA to create macro-enabled copies, then add the Ribbon button and VBA project protection separately.
The example adds a demonstration macro and Ribbon callback to every PPTX file in one folder. Replace the demonstration macro with your own code and test it on a small set of files first.
Adding VBA code does not automatically add Ribbon XML or set a VBA project password. Those are separate steps.
In PowerPoint, open File > Options > Trust Center > Trust Center Settings > Macro Settings and select Trust access to the VBA project object model. Your organization's policies may prevent this setting from being changed.
In Excel, open the VBA editor with Alt+F11, choose Insert > Module, and paste the script below. Change the two folder paths, create the output folder, and run AddMacrosToPresentations. Use a dedicated output folder with no same-named files; review errors before running the script again. Sub AddMacrosToPresentations() Dim ppt As Object, pres As Object, modl As Object Dim inputFolder As String, outputFolder As String Dim fileName As String, outputName As String, macroCode As String inputFolder = "C:\Input\" outputFolder = "C:\Output\" macroCode = "Public Sub RunMyMacro()" & vbCrLf & _ " MsgBox ""Macro is ready.""" & vbCrLf & _ "End Sub" & vbCrLf & _ "Public Sub RibbonCallback(control As IRibbonControl)" & vbCrLf & _ " RunMyMacro" & vbCrLf & _ "End Sub" Set ppt = CreateObject("PowerPoint.Application") ppt.Visible = True fileName = Dir(inputFolder & "*.pptx") Do While fileName <> "" Set pres = ppt.Presentations.Open(inputFolder & fileName) Set modl = pres.VBProject.VBComponents.Add(1) modl.CodeModule.AddFromString macroCode outputName = outputFolder & Left(fileName, Len(fileName) - 5) & ".pptm" pres.SaveAs outputName, 25 pres.Close fileName = Dir() Loop ppt.Quit End Sub
Open each output PPTM file in an Office RibbonX editor. Add a Custom UI part and insert a custom image using the editor's icon-import feature. Use the image identifier assigned by the editor in place of customIcon in this XML: <customUI xmlns="http://schemas.microsoft.com/office/2009/07/customui"><ribbon><tabs><tab id="myTab" label="My Tools"><group id="myGroup" label="Macros"><button id="myButton" label="Run Macro" image="customIcon" size="large" onAction="RibbonCallback"/></group></tab></tabs></ribbon></customUI>. Save the file in the editor. For multiple files, repeat this packaging step or automate the Custom UI part and image relationship with an appropriate Office package tool.
Open the PPTM in PowerPoint, press Alt+F11, select its VBA project, and choose Tools > VBAProject Properties > Protection. Select Lock project for viewing, enter and confirm a password, then save and close the presentation. Reopen it to apply and verify the lock.
Reopen the PPTM in PowerPoint, enable macros only if you trust the file, and select My Tools > Run Macro. Confirm that the custom icon appears and the demonstration message opens.
Work with presentations and spreadsheets in WPS Office
WPS Office is a free, lightweight alternative with a familiar interface for working with Microsoft Office files. Open existing presentations and spreadsheets for a straightforward migration; use Microsoft Office for the VBA project and Ribbon customization workflow described above.

Frequently Asked Questions
Why must I save the presentation as a PPTM file?
PPTM is PowerPoint's macro-enabled presentation format. Use File > Save As and select PowerPoint Macro-Enabled Presentation, or use the PPTM format when saving through VBA.
Why does Excel VBA fail when it tries to add a PowerPoint module?
Check PowerPoint's File > Options > Trust Center > Trust Center Settings > Macro Settings and enable Trust access to the VBA project object model if your organization permits it. Also check whether the target VBA project is locked.
Can Excel VBA set the PowerPoint VBA project password through a supported object-model property?
The VBA project object model does not expose a supported password-setting property. Set the password separately in the PowerPoint VBA editor under Tools > VBAProject Properties > Protection.
Why does my custom Ribbon button not run the macro?
Check that the button's onAction value matches a public callback in the presentation's VBA module and that the callback accepts an IRibbonControl argument. Reopen the PPTM in PowerPoint and allow trusted macros before testing.




