PPT2000: How to Create Application Level Event Handlers
ID: Q234184
|
The information in this article applies to:
-
Microsoft PowerPoint 2000
SUMMARY
If you want a particular event handler to run when a certain event is
activated, you can write an event handler for the Application object. Event
handlers for the Application object are global. As long as
Microsoft PowerPoint is open, the event handler will run when the appropriate event occurs, regardless of which presentation is active when the event occurs.
This article describes how to create an Application level event handler and
provides an example.
MORE INFORMATIONMicrosoft provides programming examples for illustration only, without
warranty either expressed or implied, including, but not limited to, the
implied warranties of merchantability and/or fitness for a particular
purpose. This article assumes that you are familiar with the programming
language being demonstrated and the tools used to create and debug
procedures. Microsoft Support professionals can help explain the functionality
of a particular procedure, but they will not modify these examples to
provide added functionality or construct procedures to meet your specific
needs. If you have limited programming experience, you may want to contact
the Microsoft fee-based consulting line at (800) 936-5200. For more
information about the support options available from Microsoft, please see
the following page on the World Wide Web:
http://www.microsoft.com/support/supportnet/overview/overview.asp NOTE: The following macro examples only work from within the PowerPoint application. Visual Basic for Applications macros are not supported by the Microsoft PowerPoint Viewer. For additional information, please see the following article in the Microsoft Knowledge Base:
Q230746 PPT: Viewer: Presentation Macros Don't Run
To create an Application-level event handler, follow these steps:
- Use the WithEvents keyword to declare a variable for the Application object. The WithEvents keyword can be used to create an object variable that responds to events that are activated by an ActiveX object (such as the Application object).
NOTE: WithEvents is valid only in a class module.
- Create the procedure for the specific Application event. For example, you could create a procedure for the SlideShowNextSlide, PresentationOpen, or SlideShowBegin event of the object that you declared using WithEvents.
- Create and run a procedure that starts the event handler.
The following example uses these steps to set up a global event handler
that displays a message box when you resize any window (the
event activating the event handler).
Creating and Initiating the Event Handler
To create and initiate the event handler, follow these steps:
- Create a new Presentation.
- Start the Visual Basic Editor.
- Click Class Module on the Insert menu.
This will insert a module titled "Class1" into your project.
- Type the following line of code in the Class1 module:
Public WithEvents appevent As Application
The WithEvents keyword makes the appevent variable available in the Object list in the Class1 module window.
- In the Class1 module window, click to select appevent in the Object list.
- In the Class1 module window, click the Procedure list and then click SlideShowBeginin the list.
This will add the following code to the Class1 module sheet:
Private Sub appevent_SlideShowBegin(ByVal Wn As SlideShowWindow)
End Sub
- Add code to the Class1 module sheet so that it appears as
follows:
Public WithEvents appevent As Application
Private Sub appevent_SlideShowBegin(ByVal Wn As SlideShowWindow)
MsgBox "you started a slideshow"
End Sub
Next, create an instance of the class, and then set the appevent object of the instance of the Class1 to Application. This is because when you declare a variable (WithEvents) at design-time, there is no object associated with it. A WithEvents variable is just like any other object variable. You have to create an object and assign a reference to the object to the WithEvents variable.
- On the Insert menu, click Module to insert a general type module sheet into your project.
- In this module sheet, type the following code:
Dim myobject As New Class1
Sub StartEvents()
Set myobject.appevent = Application
End Sub
- Run the "StartEvents" macro.
You have just set the event handler to run each time you start a SlideShow window in Microsoft PowerPoint.
- On the File menu, click Close and Return to Microsoft PowerPoint.
- Start a slideshow.
A message box with "you started a slideshow" will be displayed.
How to Disable the Event Handler
If you close the presentation that contains the previous project, the application level event handler will be disabled. To programmatically turn off the event handler, follow these steps:
- Start the Visual Basic Editor.
- In the module you created in step eight of the previous list of steps, type in the following macro code:
Sub StopEvents()
Set myobject.appevent = Nothing
End Sub
- Run "StopEvents."
- On the File menu, click Close and Return to Microsoft PowerPoint.
- Start a slideshow.
A message box will not be displayed.
For more information about running sample code, please see the following article in the Microsoft Knowledge Base:
Q173707 OFF97: How to Run Sample Code from Knowledge Base Articles
REFERENCESFor more information about Class Modules, in the Visual Basic Editor, click
Microsoft Visual Basic Help on the Help menu, type "Module and Class
Module Commands" in
the Office Assistant or the Answer Wizard, and then click Search to
view the topic.
Additional query words:
vba visual basic visualbasic application events triggers fire trip get see find doit procedure launch begin start
Keywords : kbdta kbdtacode OffVBA KbVBA
Version : WINDOWS:2000
Platform : WINDOWS
Issue type : kbhowto
|