Table of contents
TOC
折叠目录
展开目录

使用应用程序对象事件Use Events with the Application Object

office 365 dev account|上次更新日期: 2018/3/13
|
2 参与人员

若要创建应用程序对象的事件的事件处理程序,您需要完成以下三个步骤:To create an event handler for an event of the Application object, you need to complete the following three steps:

  1. 在类模块中声明一个对象变量以响应事件。Declare an object variable in a class module to respond to the events.

  2. 编写特定的事件过程。Write the specific event procedures.

  3. 在另一模块中初始化声明的对象。Initialize the declared object from another module.

声明对象变量Declare the Object Variable

可以编写应用程序对象的事件过程之前,必须创建新的类模块,并声明具有事件的类型应用程序的对象。例如,假设一个新的类模块创建和调用 EventClassModule。新的类模块包含以下代码。Before you can write procedures for the events of the Application object, you must create a new class module and declare an object of type Application with events. For example, assume that a new class module is created and called EventClassModule. The new class module contains the following code.

Public WithEvents App As Application

编写事件过程Write the Event Procedures

用事件声明新对象后,它将出现在类模块中的对象列表,您可以编写新的对象的事件过程。(在对象列表中选择新对象时,该对象的有效事件是列表中列出过程。)从过程列表中; 选择一个事件类模块中添加一个空过程。After the new object is declared with events, it appears in the Object list in the class module, and you can write event procedures for the new object. (When you select the new object in the Object list, the valid events for that object are listed in the Procedure list.) Select an event from the Procedure list; an empty procedure is added to the class module.

Private Sub App_NewPresentation()

End Sub

初始化声明的变量Initializing the Declared Object

运行该过程之前,您必须与应用程序对象连接 (在此示例应用程序) 的类模块中声明的对象。你可以用下面的代码中的任何模块。Before the procedure will run, you must connect the declared object in the class module (App in this example) with the Application object. You can do this with the following code from any module.

Dim X As New EventClassModule
Sub InitializeApp()
    Set X.App = Application
End Sub

运行 InitializeApp 过程。此过程运行之后,事件发生时将运行在 Microsoft Office PowerPoint应用程序对象中,类模块点的应用程序对象和类模块中的事件过程。Run the InitializeApp procedure. After the procedure is run, the App object in the class module points to the Microsoft Office PowerPoint Application object, and the event procedures in the class module will run when the events occur.

© 2018 Microsoft