Make your VBA code in PowerPoint respond to events
PowerPoint 97 doesn't support events, so it can't automatically run macros when a presentation opens, at print time, etc. PowerPoint 2000 and later support events, so there are some interesting possibilities. PowerPoint MVP Shyam Pillai has graciously provided this tutorial.
See Shyam's download section for his AutoEvents add-in for PowerPoint - it does all the tricky stuff for you, and it's free. I'm not giving you the direct link to the file because there's a ton of other useful stuff on the downloads page. You owe it to yourself to have a look!
There's also an Event Handler code sample (referred to below) on the same page. Search for "Event Handler Demo in PowerPoint 2000+".
Chirag Dalal has also written an excellent event capture add-in and has posted this very useful PowerPoint Event Table that lists events, versions of PPT that support them and explains the order in which the events fire.
Events in PowerPoint 2000 are limited to Application level as compared to those available across Excel and Word. They can be essentially grouped into 3 categories:
Presentation events:
- NewPresentation
- PresentationOpen
- PresentationNewSlide
- PresentationSave
- PresentationPrint
- PresentationClose
SlideShow events
- SlideShowBegin
- SlideShowEnd
- SlideShowNextBuild
- SlideShowNextSlide
Window events
- WindowActivate
- WindowBeforeDoubleClick
- WindowBeforeRightClick
- WindowDeactivate
- WindowSelectionChange
Later versions of PPT have introduced a few new events. To see the available events in the version you're using:
- Press Alt+F11 to open the VBA editor (the IDE).
- In the IDE, press F2 to open the object browser.
- Scroll down to Application in the Classes list and click it.
- In the Members window on the right, look for lightning bolt icons. Those are the events supported by your version of PPT.
How do you create a event handler?
An object that raises events is called an event source. To handle the events raised by an event source, you can declare a variable of the object's class using the WithEvents keyword. "WithEvents" keyword is used to create an object variable that responds to events that are activated by an ActiveX object.
Things to be noted about WithEvents:
- WithEvents keyword is valid only in a class module
- WithEvents variable cannot be a generic object variable, the class name has to be used.
- You cannot declare a WithEvents variable As New.
- You cannot create arrays of WithEvents variables.
Steps to create the handler:
- Open a new presentation.
- Launch VBE (Alt+F11). Insert a new class into the project
- Type the line given below:
- Now click on the Object combo box. The Object Variable PPTEvent will be listed there. Select PPTEvent. In the adjoining Procedure combo box select from one of the choices of events listed and add appropriate code to the events that you wish to handle.
- Now insert a new module sheet. cEventClass is the name of the class I created. Hence add the following line:
- To terminate the eventhandler set the refernce of the object variable to Nothing (just like any other object variable) like this:
Public WithEvents PPTEvent As Application
The WithEvents keyword specifies that the variable PPTEvent will be used to handle an object's (application) events.
Dim cPPTObject As New cEventClass
This is done because we need to instantiate an object of the cEventClass and then set the reference of the object variable PPTEvent to the powerpoint application.. The reference is set in the TrapEvents routine by the line given below:
Set cPPTObject.PPTEvent = Application
In the EventHandler Demo, look up the routine TrapEvents for this statement. Execute it. This will activate the eventhandler
Set cPPTObject.PPTEvent = Nothing
In the file this statement will be encountered in the ReleaseEvent routine.
Things to be noted...
- An Event handler cannot be set automatically. To set an event handler when PowerPoint starts up you still need to rely on the Auto_Open macro of an add-in to instantiate the event handler.
- An event handler is disabled as soon as the presentation/add-in in which it resides is closed/unloaded.