<

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x

Initializing PowerPoint Events with VBA and Ribbon XML

Published on
7,205 Points
2,505 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
Comment
  • 3
  • 3
  • 2
  • +1
9 Comments

Expert Comment

by:whftherb
Jamie - this is excellent!  I've been looking for something like this for... years?

Question:  Have you ever dabbled in the web viewer in PPT?  Specifically, have you ever loaded in a webpage and then asked PPT to scroll the page down at a slow speed?

Just curious is this is even possible.  I've tried, and failed with some third part add-ins that promised success.  If it can be done, can you point me where to start, please?

Thanks.

Hoib
0
LVL 14

Author Comment

by:Jamie Garroch
Hi Hoib.

I recently answered another question on using the ActuveX web browser control. This might help you:

http://www.experts-exchange.com/Software/Office_Productivity/Office_Suites/MS_Office/PowerPoint/Q_28548504.html

Jamie.
0

Expert Comment

by:whftherb
....Wow.  A fastball - strike - low and at the knees....!

Perfect!  I've got lot to play with and sort through now whereas an hour ago...zilch!

Thanks ever so much.

Hoib
0
Amazon Web Services

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

Expert Comment

by:Kelvin4
Thank you, Jamie
This should help me a lot..
Kelvin
0

Expert Comment

by:Antonio King
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
0
LVL 14

Author Comment

by:Jamie Garroch
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
0

Expert Comment

by:Antonio King
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
0
LVL 14

Author Comment

by:Jamie Garroch
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

1

Expert Comment

by:Antonio King
Thanks, that works perfect!
0

Featured Post

Why Diversity in Tech Matters

Kesha Williams, certified professional and software developer, explores the imbalance of diversity in the world of technology -- especially when it comes to hiring women. She showcases ways she's making a difference through the Colors of STEM program.

Join & Write a Comment

The viewer will learn how to edit animations within the presentation, incorporate sound, and set everything up with timing.
The viewer will learn how to  create a slide that will launch other presentations in Microsoft PowerPoint. In the finished slide, each item launches a new PowerPoint presentation and when each is finished it automatically comes back to this slide: …

Keep in touch with Experts Exchange

Tech news and trends delivered to your inbox every month