Microsoft KB Archive/286310

= Description of behaviors of AutoExec and AutoOpen macros in Word =

Article ID: 286310

Article Last Modified on 3/30/2007

-

APPLIES TO


 * Microsoft Office Word 2007
 * Microsoft Office Word 2003
 * Microsoft Word 2002 Standard Edition

-



This article was previously published under Q286310



SUMMARY
You can use the AutoExec and AutoOpen 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.



AutoExec
An AutoExec macro runs when Word starts if the AutoExec macro is saved as part of the default (Normal.dot) template, or if it is saved as part of a global add-in. By using an AutoExec macro, you can make adjustments when Word starts but before a document is created or loaded. For example, you can use an AutoExec macro to change the default directory when Word starts.

You can suppress the AutoExec 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, click Start, click Run, type either winword /m or winword /embedding, and then click OK.

Another method you may use to suppress an AutoExec macro is to press the Shift key while Word starts. AutoExec macros are also suppressed when Word is started from an Automation client.

AutoOpen
The AutoOpen macro runs after you open a new document. AutoOpen runs when you open a document in the following ways:
 * Use the Open command on the File menu.
 * Use the FileOpen or FileFind commands.
 * Select a document from the Most Recently Used (MRU) list on the File menu.

When a document is opened, an AutoOpen macro runs if the AutoOpen 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 AutoOpen macro does not run when it saved as part of a global add-in.

You may prevent an AutoOpen macro from running by holding down the Shift key when you open a document.

Create an AutoExec Macro and an AutoOpen Macro
Word 2002 or Word 2003  Create a new blank document in Word. On the Tools menu, point to Macro and then click Security. In the Security dialog box, click the Security Level tab and then select Medium. Click the Trusted Sources tab, select Trust all installed Add-ins and Templates, and then click OK. Create an AutoExec macro. To do this, follow these steps:  On the Tools menu, point to Macro and then click Record New Macro. In the Record Macro dialog box, type AutoExec under Macro name, and then click OK. By default, the macro is saved in the Normal template. A small two-button command bar appears on your Word document or elsewhere on the Word desktop. Find the command bar and then click the square (Stop Recording) button to stop recording. On the Tools menu, point to Macro and then click Macros. Select AutoExec in the list of macros and then click Edit.</li>  Add the following code to the AutoExec macro: MsgBox &quot;You're seeing the AutoExec macro in action&quot;, vbMsgBoxSetForeground </li> On the File menu in the Microsoft Visual Basic Editor, click Save Normal, and then close the Visual Basic Editor.</li></ol> </li> Follow the same steps that you used for the AutoExec macro to create an AutoOpen macro. This time, use the AutoOpen expression to replace the AutoExec expression in each step.</li> Save the document as C:\Yourfile.doc, close the document, and then exit Word.</li> Open the document in Word by using the different methods that are described in the table below. Observe when the macros run and when the macros do not run.</li></ol>

Word 2007 <ol> Create a new blank document in Word.</li> Click the Microsoft Office Button, and then click Word Options.</li> Click Trust Center, click Trust Center Settings, and then click Trusted Locations.</li> Add the trusted locations that you want, and then click OK two times.</li> Create an AutoExec macro. To do this, follow these steps: <ol style="list-style-type: lower-alpha;"> Click the Developer tab, and then click Record Macro in the Code group.</li> In the Record Macro dialog box, type AutoExec under Macro name, and then click OK. By default, the macro is saved in the Normal template. Click Stop Recording in the Code group.</li> On the Developer tab, click Macros in the Code group. Select AutoExec in the list of macros, and then click Edit.</li>  Add the following code to the AutoExec macro: MsgBox &quot;You're seeing the AutoExec macro in action&quot;, vbMsgBoxSetForeground </li> In Microsoft Visual Basic Editor, click Save Normal on the File menu, and then close Visual Basic Editor.</li></ol> </li> <li>Follow the same steps that you used for the AutoExec macro to create an AutoOpen macro. This time, use the AutoOpen expression to replace the AutoExec expression in each step.</li> <li>Save the document as C:\ .doc, close the document, and then exit Word.</li> <li>Open the document in Word by using the different methods that are described in the table in the &quot;Macro Behaviors in Different Situations&quot; section. Notice when the macros run and when the macros do not run.</li></ol>

Macro Behaviors in Different Situations
The following table summarizes the behavior of these two macros when Word is started, or when a document is opened by various means: <pre class="fixed_text">Action                                         AutoExec       AutoOpen --- Start Word with a blank document by typing     Runs           Does not run the following at a command prompt: Winword.exe

Start Word with a saved document by typing     Runs           Runs the following at a command prompt: Winword.exe C:\<Yourfile>.doc

Embed a Word document in an OLE container      Does not run   Runs by typing the following at a command prompt: WinWord.exe /embedding C:\<Yourfile>.doc

Use code to automate Word and open             Does not run   Runs C:\<Yourfile>.doc by using the following code: Set oWord = CreateObject(&quot;Word.Application&quot;) oWord.Visible = True oWord.Documents.Open &quot;C:\YourFile.doc&quot;

Browse to the document in Internet Explorer    Does not run   Runs or the WebBrowser control

<div class="references_section">