<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

PowerPoint Events

Published on
15,315 Points
7,515 Views
3 Endorsements
Last Modified:
Approved
People often ask "How do I make this macro run every time I show a new slide or add a new slide etc." This means making PowerPoint respond to that EVENT and, unlike in Excel, it's not that easy!

First, I would avoid using the pseudo events left over from version 97, e.g., Sub OnSlideShowpageChange. Pseudo events are not properly supported in later versions of PowerPoint and so will work erratically.

Also, naming your sub Auto_Open will not work in a standard presentation but will in a PPA (version 97-2003) or a PPAM (version 2007/2010) AddIn.

So, let's create a simple AddIn...

1. Add a Class


A. In the VB editor INSERT >> CLASS MODULE

B. If the properties window isn't already visible, press F4 or View > Properties window to show it. Use this to rename the class module to something more memorable. We used cNewSlideClass.

C. Add the code:
Public WithEvents PPTEvent As Application

Open in new window


2. Create an Object Based on the Class


A. Insert a standard module (INSERT >> MODULE) and declare the object

B. (change the names as appropriate)
Public objNewSlide as New cNewSlideClass

Open in new window


3. Initialise the Object


A. In the normal module create a new sub call Auto_Open. Remember this will only fire in an AddIn and only when the AddIn loads.

B. Add the code as below:
Sub Auto_Open()
    objNewSlide.PPTEVENT = Application
End Sub

Open in new window


4. The Event


A. Back in the class module you will see two drop down menus at the top of the page.
Change the left one to PPTEVENT; the right, select the event to fire your macro. In our case PresentationNewSlide.

B. Between the lines of code auto inserted add the code you want to run.
Here is ours:
Private Sub PPTEvent_PresentationNewSlide(ByVal Sld As Slide)
    MsgBox "You inserted slide " & Sld.SlideIndex
End Sub

Open in new window

Not that useful, but just an example!

Finally save as a PPA or PPAM AddIn, depending on your version, and you're good to go!
3
Comment
Author:JSRWilson
1 Comment
LVL 14

Expert Comment

by:Jamie Garroch
This is a great article JSRWilson. I'd just like to add that if a developer is looking for a cross-platform compatible solution for application events that will work on PC and Mac versions of PowerPoint, there doesn't appear to be one. Why?

Application Events are supported by PC versions of PowerPoint only (as of PowerPoint:mac 2011, and possible PowerPoint:mac 2016)
Psuedo Events are supported by PC and Mac versions but they are unreliable and in many cases will cause PowerPoint to crash

Finally, to get around the Auto_Open limitation of only firing within an add-in (as opposed to a presentation), you can use this article to solve that issue (again, only for PC versions of PowerPoint supporting the ribbon):

http://www.experts-exchange.com/articles/17410/Initializing-PowerPoint-Events-with-VBA-and-Ribbon-XML.html
0

Featured Post

HTML5 and CSS3 Fundamentals

Build a website from the ground up by first learning the fundamentals of HTML5 and CSS3, the two popular programming languages used to present content online. HTML deals with fonts, colors, graphics, and hyperlinks, while CSS describes how HTML elements are to be displayed.

Join & Write a Comment

This video teaches viewers how to create handouts from their slides and helps them decide how many slides to include per handout.
The viewer will learn how to edit the master slide. They will also learn how to combine multiple themes into one master slide to use them in their presentation.

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month