Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 !

like image 807
Laurent Crivello Avatar asked Jul 03 '12 07:07

Laurent Crivello


People also ask

Can you autoplay on PowerPoint?

Setting up autoplay requires a few steps, including creating the presentation, optionally adding audio like a voiceover or music and rehearsing timings of slides. PowerPoint autoplay is a relatively easy tool to use that can make your presentations look polished and professional.


2 Answers

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.

like image 91
Jon Peltier Avatar answered Oct 15 '22 15:10

Jon Peltier


I am using PowerPoint 2016 so I can't speak for earlier versions although the documentation indicates this works for PowerPoint versions 2016, 2013, 2010, 2007. This method DOES NOT require any add-ins or application hooks to handle events.

Reference link: https://support.office.com/en-us/article/command-line-switches-for-microsoft-office-products-079164cd-4ef5-4178-b235-441737deb3a6?ocmsassetID=HA010153889&CTT=1&CorrelationId=ea39d200-aa81-4d6e-8302-afff4c65859e&ui=en-US&rs=en-US&ad=US#ID0EAABAAA=PowerPoint,_PowerPoint_Viewer)

Start PowerPoint from command line and use the /M switch to have PowerPoint run a specified macro when it starts a named presentation file.

The easiest way to do this is to create a shortcut to the PowerPoint application. Then go to the Properties window for the shortcut and select the Shortcut tab. Next, add the /M switch, your presentation file name (including path), and the name of the macro to run (case sensitive and must be part of the presentation) to the end of the Target field. Double-click the shortcut and voila!

Example: My presentation is C:\myPPTpres.pptm and the macro is Run_Slide_Show so I will need to add /M "C:\myPPTpres.pptm" "Run_Slide_Show" to the end of the existing text in the Target field.

"C:\Program Files (x86)\Microsoft Office\root\Office16\POWERPNT.EXE" /M "C:\myPPTpres.pptm" "Run_Slide_Show"

Make sure you are running a macro-enabled version of the presentation (in 2016 it has the .pptm extension.)

Important note if you are trying to start a slide show automatically using the macro. Add a delay of one or more seconds at the very beginning of your macro to allow the application to finish its startup sequence. If you do not the slideshow will start but the application will steal focus as it completes its startup, pushing your slideshow to the background.

like image 27
smitty2781 Avatar answered Oct 15 '22 15:10

smitty2781