Certain code in VBA requires initialization, such as application events. The app initialization is often triggered by the Auto_Open
sub which is a special procedure that runs when an add-in loads. More significantly, this sub does not trigger if the VBA is loaded inside a PowerPoint file such as a .pptm file. So how does one get round this?
In this article we'll show you how to create a PowerPoint file, which could be a macro-enabled presentation (.pptm), slideshow (.ppsm) or template (.potm), which contains the necessary VBA code to initialise events based on the ribbon load event in the self-contained XML part of the file.
This sample demo initialises application events when the PowerPoint file opens, without:
- Installing an add-in (that initiates code with Auto_Open)
- User interaction (to initialise the app events)
How to Create the PowerPoint VBA/XML/PPTM Project
- Open a new presentation, press Alt+F11 and Insert a class module, naming it clsAppEvents and insert the following code:
Public WithEvents App As Application
- Click the (General) drop down and select App. You can then click the right hand drop down to select the event procedure you need for your code, for example:
Private Sub App_WindowSelectionChange(ByVal Sel As Selection)
MsgBox "Selection Changed", vbInformation + vbOKOnly, "App Event Class"
- Insert a standard module and insert the following code:
Public oEH As New clsAppEvents
Public Sub onRibbonLoadAppEvents()
Set oEH.App = Application
- Save the presentation as a PPTM file and then close PowerPoint.
- Download and install the Custom UI Editor for Microsoft Office and after running it:
- Open your PPTM file in the UI Editor
- Click Insert / Office 2007 Custom UI Part
- Insert the following code:
<customUI onLoad="onRibbonLoadAppEvents" xmlns="http://schemas.microsoft.com/office/2006/01/customui">
Download an example PPTM file
- Save the file and quit the Custom UI editor
- Start PowerPoint and open your PPTM file to see events firing!