Our community of experts have been thoroughly vetted for their expertise and industry experience.
Eating, sleeping, breathing PowerPoint and VBA at BrightCarbon
Published:
Browse All Articles > Initializing PowerPoint Events with VBA and Ribbon XML
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:
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
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?
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 = msoFalseEnd Sub
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.
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:
Comments (9)
Commented:
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
Author
Commented: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
Commented:
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
Author
Commented:Open in new window
So it becomes this:
Open in new window
Commented:
View More