from VBA help:
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 "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. The only exception is the AutoExec macro, which will not run automatically unless it is stored in the Normal template 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 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 "ThisDocument." Use the following steps to create an event procedure.
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 "New document was created"
End Sub
The following example shows a Close event procedure in a document project that runs only when that document is closed.
Private Sub Document_Close()
MsgBox "Closing the document"
End Sub
Unlike auto macros, event procedures in the Normal template don't have a global scope. For example, event procedures in the Normal template only occur if the attached template is the Normal template.
If an auto macro exists in a document and the attached template, only the auto macro stored in the document will execute. If an event procedure for a Document event exists in a document and its attached template, both event procedures will run.
Note For information on creating event procedures for the Application object, see Using Events with the Application Object.
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:
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
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.