|
|
|
|
|
|
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
|
Public WithEvents PPTEvent As Application
Private Sub PPTEvent_AfterNewPresentation(ByVal pres As Presentation)
pres.Saved = msoTrue
pres.Close
Set cPPTObject.PPTEvent = Nothing
End Sub
Public cPPTObject As New cEventClass
Sub Auto_Open()
Set cPPTObject.PPTEvent = Application
End Sub
|
How to use:
|
- Create a new blank presentation and open the VB Editor (Alt-F11)
- Add a new class module and name it "cEventClass"
- Paste in the varible declaration for the cEventClass
- Select PPTEvent from the object list (the left dropdown list above the code window)
- The event list (the right dropdown list above the code window) will now display all the available events
- Select the AfterNewPresentation event and add the code for it
- Add a new standard module
- Paste in the standard module code
- Go to the Debug menu and select compile VBA Project
- Close the VB Editor
- Save the presentation as an AddIn (.ppa)
|
Test the code:
|
- In PowerPoint, load the new AddIn from Tools|Addins
- Close PowerPoint
- 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 226 times.
|
|