Zsolt
Zsolt

Reputation: 3283

How to write auto macros in PowerPoint 2013?

I'm trying to write a macro which gets automatically executed when I open my PowerPoint presentation in PowerPoint 2013. According to this article, and this StackOverflow answer this can be done with writing a VBA subroutine with the name "Auto_Open":

Sub Auto_Open()
   MsgBox ("Hello World!")
End Sub

I guess, this is a very basic stuff, but still this doesn't work for me. In the Trust center, I enabled all macros, and the "Trust access..." is also checked in.

I'm using PowerPoint 2013. Is it possible that Microsoft doesn't support auto macros in PowerPoint 2013? I haven't found any info about this on the internet, only for Word 2013.

Upvotes: 2

Views: 8830

Answers (3)

Jon Peltier
Jon Peltier

Reputation: 6063

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: although I thought of using this approach and have used it in Excel, I didn't until I first encountered it on the PPT Alchemy web site: Run Code When PowerPoint Opens.

Upvotes: 0

Steve Rindsberg
Steve Rindsberg

Reputation: 14809

An Auto_Open subroutine will fire automatically when an ADD-IN containing it is loaded, but the same subroutine in a regular PowerPoint file will not run automatically. The article on mvps.org does NOT suggest otherwise; it's about an add-in that traps events and can then call macros that you write, but again, it requires that an add-in be loaded.

An add-in can trap presentation open and other events, and can fire code that checks the presentation to determine whether it should do further processing or not ... IOW, it could check to see if this is your presentation or some random one (that it shouldn't touch).

Upvotes: 2

Dawid
Dawid

Reputation: 786

It seems to me that there is no simple way to automatically run macros in PowerPoint.

There is a possibility to run the macro using Auto_Open, but with very limited functionality.

1.Create *.pptm file with code:

Sub Auto_Open()
MsgBox "Hello"    
End Sub

2.Save file as Add-in -> *.ppam

3.Open you PowerPoint document and add this add-in

Every time you run PowerPoint will automatically run this add-in. In add-in works Auto_Open :)

Writing about writing about "small functionality" I meant (http://skp.mvps.org/ppafaq.htm#14):

Two macros are fired automatically within an add-in. Auto_Open and Auto_Close. Auto_Open is fired when the add-in is loaded and Auto_Close fired when the add-in is being unloaded. You can use them to do preprocessing, creating menu items, setting up event handlers etc or performing cleanup upon exiting.

Upvotes: 1

Related Questions