<

Initializing PowerPoint Events with VBA and Ribbon XML

Published on
8,033 Points
3,333 Views
2 Endorsements
Last Modified:
Jamie Garroch
Eating, sleeping, breathing PowerPoint and VBA at YOUpresent.co.uk

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
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