Word VBA(Events)

Microsoft Word VBA (3)

Word Events

在Word中只有Application object和Document object响应事件: Events With Application object :

NewDocument, DocumentOpen,

DocumentBeforeClose, DocumentBeforePrint, DocumentBeforeSave WindowActivate, WindowDeactivate, WindowSelectionChange, WindowBeforeDoubleClock, WindowBeforeRightClick, Quit

Events With Document object :

New, Open, Close

此外,在Document上的ActiveX Cobtrols也响应事件。 对每一FormField可以指定On Entry和On Exit宏,相当于 GotFocus和LostFocus事件过程。当然在Word VB Project中建立的UserForms及其上的Controls也响应事件。

Events: When you open a document 当Open MyDoc1.doc文档时,发生以下事件:

1. Document_Open

2. App_DocumentChange :

3. App_DocumentOpen : C:\\…\\MyDoc1.doc

如果Order4.Doc有AttachedTemplate,则当Open该Doc时,发生以下事件: 1. Template’s Document_Open(Doc打开也触发模板的Open事件。)

2. App_DocumentChange (如果这不是第一个打开的文档,发生App_DocumentChange事件) 3. Document’s Document_Open

4. App_DocumentOpen : C:\\My Documents\\Order4.doc

Events: When you close a document

当Close文档时,发生以下事件:

1. App_DocumentBeforeClose:C:\\…\\MyDoc1.doc 2. Document_Close

3. 对话框Save Changes ?

4. 如果Save, App_DocumentBeforeSave :C:\\…\\MyDoc1.doc (如果不Save changes则不发生App_DocumentBeforeSave。)

如果Order4.Doc有AttachedTemplate,则当Close该Doc时,发生以下事件: 1. App_DocumentBeforeClose:C:\\My Documents\\Order4.doc

2. Template’s Document_Close (Doc关闭也触发模板的Close事件。) 3. Document’s Document_Close

4. 对话框Save Changes to .Doc ?

5. Yes: App_DocumentBeforeSave: C:\\My Documents\\Order4.doc 6. 对话框Save Changes to .Dot ?

7. Yes: App_DocumentBeforeSave: C:\\WINDOWS\\Application Data\\Microsoft\\Templates\\Order.dot 8. 关闭Word文档窗口

注:如果你不希望Save模板(即使有Changes),可以在最后一个App_DocumentBeforeSave事件

之前,例如在Template’s Document_Close事件里,执行以下语句:

ActiveDocument.AttachedTemplate.Saved = True

(不要用DocumentBeforeSave事件里的Cancel=True,它不关闭.Doc窗口。)

如果你希望在单独打开模板.dot文件(作为通常的Document文件)时仍可以保存对模板的 Changes,可在Template’s Document_Close事件里加上以下语句:

If ActiveDocument.FullName <> ThisDocument.FullName Then

ActiveDocument.AttachedTemplate.Saved = True 'if as an attached template End If

因为在单独打开模板.dot文件时,ActiveDocument.FullName = ThisDocument.FullName。 在模板里写ThisDocument表示该模板文件,而ActiveDocument可能是指基于该模板的doc文 件,或者模板文件本身(单独打开时)。

(不管是否单独打开,在模板里ActiveDocument.AttachedTemplate和ThisDocument总是指 同一模板, 但在.Doc里写,它们不同,而且是两种object类型:Template和 Document,虽 然它们有些共同的属性。)

Document和Template都有属性Saved。如果有Changes,系统自动置它为False。你在Close时 置它为True表示不要Save, Save Changes 对话框也不再出现。注意,DocumentBeforeSave 事件发生在Save Changes 对话框且Yes之后。

如果你不按Close命令,直接按Save命令,Close事件不发生。但App_DocumentBeforeSave 发生。此时用Cancel=True可以禁止Save,但应先判断Save的是.doc还是dot,事件过程里 有参数给出。(用OS文件属性ReadOnly最保险。)

New事件: Document_New和App_NewDocument

仅在.Dot模板里才可有Document_New事件过程。 在新建基于一模板的的Doc时发生以下事件:

Template’s Document_New

App_DocumentChange (如果原来已有某ActiveDocument文档) App_NewDocument : Document1(新文档,无.doc后缀)

Auto Macros By giving a macro a special name, you can run it automatically when you perform an operation such as starting Word or opening a document. Word recognizes the following names as automatic macros, or \

Macro name When it runs . AutoExec When you start Word or load a global template AutoNew Each time you create a new document AutoOpen Each time you open an existing document AutoClose Each time you close a document

AutoExit When you quit Word or unload a global template

Auto macros in code modules are recognized if either of the following conditions are true.

? The module is named after the auto macro (for example, AutoExec) and it contains a

procedure named \

? A procedure in any module is named after the auto macro.

Just like other macros, auto macros can be stored in the Normal template, another template, or a document. In order for an auto macro to run, it must be either in the Normal template, in the active document, or in the template on which the active document is based. The only exception is the AutoExec macro, which will not run automatically unless it is stored in one of the following: the Normal template, a template that is loaded globally through the

Templates and Add-Ins dialog box, or a global template stored in the folder specified as the Startup folder.

In the case of a naming conflict (multiple auto macros with the same name), Word runs the auto macro stored in the closest context. For example, if you create an AutoClose macro in a document and the attached template, only the auto macro stored in the document will execute. If you create an AutoNew macro in the normal template, the macro will run if a macro named AutoNew doesn't exist in the document or the attached template.

Note You can hold down the SHIFT key to prevent auto macros from running. For example, if you create a new document based on a template that contains an AutoNew macro, you can

prevent the AutoNew macro from running by holding down SHIFT when you click OK in the New

dialog box (File menu) and continuing to hold down SHIFT until the new document is displayed. In a macro that might trigger an auto macro, you can use the following instruction to prevent auto macros from running:

WordBasic.DisableAutoMacros

Using Events with the Application Object To create an event handler for an event of the Application object, you need to complete the following three steps:

? Declare an object variable in a class module to respond to the events. ? Write the specific event procedures.

? Initialize the declared object from another module.

Declare the Object Variable

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 Word.Application

Write the Event Procedures

After the new object has been declared with events, it appears in the Object drop-down list box in the class module, and you can write event procedures for the new object. (When you select the new object in the Object box, the valid events for that object are listed in the Procedure drop-down list box.) Select an event from the Procedure drop-down list box; an empty procedure is added to the class module.

Private Sub App_DocumentChange()

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 Register_Event_Handler()

Set X.App = Word.Application End Sub

Run the Register_Event_Handler procedure. After the procedure is run, the App object in the class module points to the Word Application object, and the event procedures in the class module will run when the events occur

Using Events with the Document Object The Document object supports three events: Close, New and Open. You write procedures to respond to these events in the class module named \

1. Under your Normal project or document project in the Project Explorer window,

double-click ThisDocument. (In Folder view, ThisDocument is located in the Microsoft Word Objects folder.)

2. Select Document from the Object drop-down list box. 3. Select an event from the Procedure drop-down list box. An empty subroutine is added to the class module.

4. Add the Visual Basic instructions you want to run when the event occurs.

The following example shows a New event procedure in the Normal project that will run when a new document based on the Normal template is created.

Private Sub Document_New()

MsgBox \End Sub

联系客服:779662525#qq.com(#替换为@) 苏ICP备20003344号-4