<

PowerPoint Events

Published on
15,894 Points
8,094 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
Author:John Wilson
Ask questions about what you read
If you have a question about something within an article, you can receive help directly from the article author. Experts Exchange article authors are available to answer questions and further the discussion.
Get 7 days free