Microsoft Word VBA (3)
Word Events
Word中只有Application objectDocument 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 EntryOn 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_OpenDoc打开也触发模板的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事件里,执行以下语句:
object to
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.AttachedTemplateThisDocument总是指
同一模板, 但在.Doc里写,它们不同,而且是两种object类型:Template和 Document,虽
然它们有些共同的属性。)
DocumentTemplate都有属性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_NewApp_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 n
ames as automatic macros, or "auto" macros.
      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 "Main."
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.

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。