Microsoft KB Archive/234184

= PPT2000: How to Create Application Level Event Handlers =

Article ID: 234184

Article Last Modified on 10/11/2006

-

APPLIES TO


 * Microsoft PowerPoint 2000 Standard Edition

-



This article was previously published under Q234184



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 INFORMATION
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements. NOTE: The following macro examples work only in PowerPoint. Visual Basic for Applications macros are not supported by the Microsoft PowerPoint Viewer. For additional information, click the following article number to view the article in the Microsoft Knowledge Base: 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. </li> On the Insert menu, click Module to insert a general type module sheet into your project.</li>  In this module sheet, type the following code: Dim myobject As New Class1

Sub StartEvents Set myobject.appevent = Application End Sub </li> Run the "StartEvents" macro.

You have just set the event handler to run each time you start a SlideShow window in Microsoft PowerPoint.</li> On the File menu, click Close and Return to Microsoft PowerPoint.</li> Start a slideshow.

A message box with "you started a slideshow" will be displayed.</li></ol>

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: <ol> Start the Visual Basic Editor.</li>  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 </li> Run "StopEvents."</li> On the File menu, click Close and Return to Microsoft PowerPoint.</li> Start a slideshow.

A message box will not be displayed.</li></ol>

For more information about running sample code, please see the following article in the Microsoft Knowledge Base:

173707 OFF97: How to Run Sample Code from Knowledge Base Articles

<div class="references_section">