How to auto execute a macro when opening a Powerpoint presentation?

Laurent Crivello picture Laurent Crivello · Jul 3, 2012 · Viewed 35.8k times · Source

I have a pretty basic question, but could not find the answer on internet.
In Powerpoint 2010, I have a macro that I would like to be executed everytime the Powerpoint document is opened. How to achieve this ?

Thanks !

Answer

Jon Peltier picture Jon Peltier · Jul 10, 2016

While Auto_Open doesn't run in a PowerPoint presentation, you can fake it. Add a CustomUI part to the presentation, then use the CustomUI OnLoad callback to run code when the presentation opens. The CustomUI part needs no more than just the CustomUI tags.

Get the Custom UI Editor from here: http://openxmldeveloper.org/articles/customuieditor.aspx

Open the presentation in the Custom UI Editor. Insert a CustomUI part from the Insert menu:

Add a Custom UI part

Now enter some simple RibbonX code, like this:

<customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" 
onLoad="MyOnloadProcedure" >
</customUI>

Now write your on-open procedure:

Sub MyOnloadProcedure()
    MsgBox "Hello"    
End Sub

If you have both this and the Auto_Open procedure in an add-in, Auto_Open runs first.

Full disclosure: while I thought of using this approach and have used it in Excel, I waited until I first encountered it on the PPT Alchemy web site: Run Code When PowerPoint Opens.