You can use the Auto-Exec and Auto-Open macros in Word to establish default paths, styles, links, environment conditions, and so on, at the time Word opens a new document or opens an existing document. This article discusses how these macros work and how they behave in various situations.
An Auto-Exec macro runs when Word starts if the Auto-Exec macro is saved as part of the default (Normal.dot) template, or if it's saved as part of a global add-in. By using an Auto-Exec macro, you can make adjustments when Word starts but before a document is created or loaded. For example, you can use an Auto-Exec macro to change the default directory when Word starts.
You can suppress the Auto-Exec macro in several ways. One way is to start Word from the command line by using either the /m or the /embedding switch. To use these switches, select Start, select Run, type either winword /m or winword /embedding , and then select OK.
Another method to suppress an Auto-Exec macro is to press the Shift key while Word starts. Auto-Exec macros are also suppressed when Word is started from an Automation client.
The Auto-Open macro runs after you open a new document. Auto-Open runs when you open a document in the following ways:
When a document is opened, an Auto-Open macro runs if the Auto-Open macro is saved as part of that document or if the macro is saved as part of the template on which the document is based. An Auto-Open macro doesn't run when it saved as part of a global add-in.
You can prevent an Auto-Open macro from running by holding down the Shift key when you open a document.
MsgBox "You're seeing the AutoExec macro in action", vbMsgBoxSetForeground
MsgBox "You're seeing the AutoExec macro in action", vbMsgBoxSetForeground
The following table summarizes the behavior of these two macros when Word is started, or when a document is opened by various means:
Action | Auto-Exec | Auto-Open |
---|---|---|
Start Word with a blank document by typing Runs Does not run the following at a command prompt: Winword.exe | Runs | Doesn't run |
Start Word with a saved document by typing the following at a command prompt: Winword.exe C:.doc | Runs | Runs |
Embed a Word document in an OLE container by typing the following at a command prompt: WinWord.exe /embedding C:.doc | Doesn't run | Runs |
Use code to automate Word and open Does not run Runs C:.doc by using the following code: Set oWord = CreateObject("Word.Application") oWord.Visible = True oWord.Documents. Open "C:\YourFile.doc" | Doesn't run | Runs |
Browse to the document in Internet Explorer or the WebBrowser control | Does not run | Runs |