PowerPoint

Enabling application events

Ease of Use

Intermediate

Version tested with

2000, 2002, 2003 

Submitted by:

Killian

Description:

Demonstrates how to enable application events in PowerPoint. This example uses this to close the blank presentation that's opened when PowerPoint launches. 

Discussion:

VBA is essentially event-driven, meaning that an event (such as clicking a toolbar button) triggers some code. Application events that are commomly used in Excel (opening or saving a workbook, clicking or changing a cell) are not available in PowerPoint at first glance. The most common use of of application events in PowerPoint is when a presentation is saved as an AddIn. If you include a routine called 'Auto_Open', this will be triggered when an AddIn is loaded. Additionally, write a routine called 'Auto_Close' and it will be triggered when the AddIn is unloaded. Custom code is normally distributed to PowerPoint users as AddIns and the Auto_Open/Close routines are used to build the toolbars required for the user to run the custom code itself. This example uses the 'Auto_Open' routine of an AddIn to create an new instance of a class and it is this class that is used to expose application events to VBA. The 'AfterNewPresentation' event is used to close the blank presentation that is opened when PowerPoint launches. 

Code:

instructions for use

			

'####################################################### '### EventClass code '####################################################### 'declares a variable that will handle application events Public WithEvents PPTEvent As Application 'the event that fires after a new presentation has opened 'variable "pres" is set as that new presentation Private Sub PPTEvent_AfterNewPresentation(ByVal pres As Presentation) 'close without saving pres.Saved = msoTrue pres.Close 'release the reference to the event handler 'this prevents the code from running on subsequent AfterNewPresentation events Set cPPTObject.PPTEvent = Nothing End Sub '####################################################### '####################################################### '### Standard module code '####################################################### 'declare an object that will create a new instance of the event class Public cPPTObject As New cEventClass Sub Auto_Open() 'set a application reference to the event-enabled object Set cPPTObject.PPTEvent = Application End Sub '#######################################################

How to use:

  1. Create a new blank presentation and open the VB Editor (Alt-F11)
  2. Add a new class module and name it "cEventClass"
  3. Paste in the varible declaration for the cEventClass
  4. Select PPTEvent from the object list (the left dropdown list above the code window)
  5. The event list (the right dropdown list above the code window) will now display all the available events
  6. Select the AfterNewPresentation event and add the code for it
  7. Add a new standard module
  8. Paste in the standard module code
  9. Go to the Debug menu and select compile VBA Project
  10. Close the VB Editor
  11. Save the presentation as an AddIn (.ppa)
 

Test the code:

  1. In PowerPoint, load the new AddIn from Tools|Addins
  2. Close PowerPoint
  3. Re-launch PowerPoint - there should now be no blank presentation at start-up
 

Sample File:

CloseAllUnSaved.zip 21.43KB 

Approved by mdmackillop


This entry has been viewed 221 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2014 VBA Express