PPT2000: How to Create Application Level Event Handlers (234184)



The information in this article applies to:

  • Microsoft PowerPoint 2000

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:
  1. Create a new Presentation.
  2. Start the Visual Basic Editor.
  3. Click Class Module on the Insert menu.

    This will insert a module titled "Class1" into your project.
  4. 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.
  5. In the Class1 module window, click to select appevent in the Object list.
  6. 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
    						
  7. 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.
  8. On the Insert menu, click Module to insert a general type module sheet into your project.
  9. In this module sheet, type the following code:
    Dim myobject As New Class1
    
    Sub StartEvents()
       Set myobject.appevent = Application
    End Sub
    						
  10. Run the "StartEvents" macro.

    You have just set the event handler to run each time you start a SlideShow window in Microsoft PowerPoint.
  11. On the File menu, click Close and Return to Microsoft PowerPoint.
  12. 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:
  1. Start the Visual Basic Editor.
  2. 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
    						
  3. Run "StopEvents."
  4. On the File menu, click Close and Return to Microsoft PowerPoint.
  5. 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:

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

REFERENCES

For more information about Class Modules, in the Visual Basic Editor, click Microsoft Visual Basic Help on the Help menu, type Module and ClassModule Commands in the Office Assistant or the Answer Wizard, and then click Search to view the topic.

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbdtacode kbhowto kbProgramming KB234184