Word recognizes the following five macro names that run automatically.
Macro name | When it runs |
AutoExec | When you start Word |
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 |
You create an auto macro just as you do a regular macro. The only difference is that you must give the macro one of the special auto macro names that Word recognizes. Just like other macros, auto macros can be defined either globally or for a particular template. The only exception is the AutoExec macro, which must be stored either in the Normal template or in a template in the Startup folder (the Startup folder is specified on the File Locations tab in the Options dialog box (Tools menu)).
In the case of a naming conflict, Word always runs the auto macro stored in the template attached to the active document. For example, if you define an AutoNew macro as a global macro stored in the Normal template, Word runs that macro whenever you create a new document, unless the new document is attached to a template that contains an AutoNew macro; in that case, Word runs the AutoNew macro stored in the attached template.
The following examples are intended to demonstrate some of the ways auto macros can be used. Don't worry if you don't understand the WordBasic instructions at this point. Chapters 3 and 4 describe the WordBasic language elements used in these examples.
By default, Word creates a new document based on the Normal template whenever you start Word. The following AutoExec macro replaces the new document based on the Normal template with one based on the LETTER template.
Sub MAIN FileNew .Template = "LETTER" End Sub
When you open a document, the following AutoOpen macro displays a prompt to ask if you would like to turn on revision marks; if you choose the Yes button, the macro turns on revision marks. You could modify this macro to test for different conditions. For example, you might want to turn on revision marks only if you aren't the author of the document you are opening. The macro could test for this condition and display or not display the prompt accordingly. For information on testing for conditions, see Chapter 3, "WordBasic Fundamentals."
Sub MAIN answer = MsgBox("Do you want to turn on revision marks?", 36) If answer = - 1 Then ToolsRevisions .MarkRevisions = 1 End If End Sub
The following AutoNew macro displays the Save As dialog box when you create a new document based on the template in which the AutoNew macro is stored. The comments (preceded by apostrophes) describe the purpose of each instruction; they have no effect when the macro runs.
Sub MAIN Dim FSArec as FileSaveAs 'Create the dialog record "FSArec" GetCurValues FSArec 'Place current values into FSArec choice = Dialog(FSArec) 'Display FileSaveAs dialog box If choice = -1 Then FileSaveAs FSArec 'If user chose OK, run FSA End Sub
When a document is closed, the following AutoClose macro makes a backup copy by copying the document to a file server. On the Macintosh, the path "E:\DOCS\BACKUP" would change to a Macintosh path, such as "BACKUP SERVER:DOCUMENTS:BACKUP."
Sub MAIN file$ = FileName$() 'Store filename in "file$" FileClose 1 'Close and save the file CopyFile .FileName = file$, .Directory = "E:\DOCS\BACKUP" 'Backup End Sub
You probably have certain view options you prefer; for example, you may generally like to work with the Standard and Formatting toolbars displayed. The following AutoExit macro resets those settings, so that when you start Word again you have the environment you want, regardless of what was displayed or hidden when you last quit Word. This particular example displays the Standard and Formatting toolbars, but of course you could edit the macro to specify any settings you wanted.
Sub MAIN ViewToolbars .Toolbar = "Standard", .Show ViewToolbars .Toolbar = "Formatting", .Show End Sub