Initializing PowerPoint Events with VBA and Ribbon XML

Jamie Garroch (MVP)PowerPoint Technical Consultant
CERTIFIED EXPERT
Eating, sleeping, breathing PowerPoint and VBA at BrightCarbon
Published:

Background

 

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.
 

Purpose

 

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

Open in new window


  • 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"
End Sub

Open in new window




  • Insert a standard module and insert the following code:

Public oEH As New clsAppEvents
Public Sub onRibbonLoadAppEvents()
  Set oEH.App = Application
End Sub

Open in new window


  • 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">
</customUI>

Open in new window


  • Save the file and quit the Custom UI editor
  • Start PowerPoint and open your PPTM file to see events firing!

Download an example PPTM file
 
2
4,761 Views
Jamie Garroch (MVP)PowerPoint Technical Consultant
CERTIFIED EXPERT
Eating, sleeping, breathing PowerPoint and VBA at BrightCarbon

Comments (9)

Antonio KingIT Manager

Commented:
Thanks very much for this code. Very helpful.

When I save as a .ppsm my app subs do not fire but they work fine when running a show from the file as a .pptm.
Is this because the ribbon is not loaded with a .ppsm file?

Any ideas on how I could get around this?

Many thanks
Antonio
Jamie Garroch (MVP)PowerPoint Technical Consultant
CERTIFIED EXPERT

Author

Commented:
Antonio, yes, the onLoad event in the ribbon customisation won't fire in a ppsm file because the ribbon UI is not loaded. You need to find another way to run the onRibbonLoadAppEvents procedure. You could put a rectangle covering the whole of slide 1 containing text "click to start show" and use Insert / Action to assign a macro to it like this:

Sub InitEvents(ByRef oShp As Shape)
  onRibbonLoadAppEvents
  oShp.Visible = msoFalse
End Sub

Open in new window


You could also unhide the trigger shape when the slide show ends. To prevent any save messages set ActivePresentation.Saved = msoTrue
Antonio KingIT Manager

Commented:
Thanks Jamie. Is there any solution which could be completely unattended?
I am using Windows Task Scheduler to open the PowerPoint show each day and would prefer to not have any user interaction with it.

Thanks again
Jamie Garroch (MVP)PowerPoint Technical Consultant
CERTIFIED EXPERT

Author

Commented:
Understood Antonio. How about this as an idea. You could save it as a pptm and add this to the initialisation procedure to automatically run it in slide show mode as it opens:

ActivePresentation.SlideShowSettings.Run

Open in new window


So it becomes this:

Public Sub onRibbonLoadAppEvents()
  Set oEH.App = Application
  ActivePresentation.SlideShowSettings.Run
End Sub

Open in new window

Antonio KingIT Manager

Commented:
Thanks, that works perfect!

View More

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.

Get access with a 7-day free trial.
You Belong in the World's Smartest IT Community