Modifying a Word Command

You can modify most Word commands by turning them into macros. For example, you can modify the Open command on the File menu so that instead of displaying a list of Word document files (in Windows, files ending with the .DOC filename extension), Word displays every file in the current folder.

To display the list of built-in Word commands in the Macro dialog box, you select Word Commands in the Macros Available In box. Every menu command and every command available on a toolbar or through shortcut keys is listed. Menu commands begin with the menu name associated with the command. For example, the Save command on the File menu is listed as FileSave. This is the same convention used by WordBasic statements. The WordBasic statement equivalent to the Save command, for example, is FileSave.

You can run any one of these commands by selecting it and choosing the Run button in the Macro dialog box. For example, if you select the FileSave command and choose the Run button, Word saves the current document, just as if you had chosen Save from the File menu.

Note, however, that the Create button is disabled when Word Commands is selected in the Macros Available In box. You cannot edit the commands themselves, but by giving a macro the same name as a Word command, you can replace the Word command with a macro. Then, whenever you choose the Word command using a menu, toolbar, or shortcut keys, Word runs the replacement macro instead. For example, if you create a macro called FileSave, Word runs this macro when you choose the Save command from the File menu, click the Save button on the Standard toolbar, or press Ctrl+S (Windows) or COMMAND+S (Macintosh).

To replace a Word command, you can either type its name in the Macro Name box or select it from the list of Word commands. You then select the template in which you would like to store the macro that replaces the command — either the Normal template or the one attached to the active document — and choose the Create button. Word displays a macro-editing window for the new macro.

An Example

This example takes you through the steps needed to modify the Close command on the File menu. In this example, you'll store the macro that replaces the FileClose command in the Normal template, so that the command will be modified globally — that is, for every document.

Û To modify the FileClose command

1. From the Tools menu, choose Macro.

2. In the Macros Available In box, select Word Commands.

3. In the Macro Name box, select FileClose.

4. In the Macros Available In box, select either Normal.dot (Global Template) in Windows or Normal (Global Template) on the Macintosh.

5. Choose the Create button.

The FileClose macro now appears in a macro-editing window.

As the previous illustration shows, Word automatically inserts the instruction FileClose in the new macro. Any macro with the same name as a Word command initially contains one or more lines that run the Word command the macro replaces. (The macro also inherits the Word command's description, which appears in the Description box in the Macro dialog box.)

You can edit the new macro in many ways. For example, you could add a FileSave instruction:


Sub MAIN
    FileSave
    FileClose
End Sub

To test the new command, open an existing document, change it in some way, and then close it (using the Close command on the File menu). The original Word command would prompt you to save your document if it contained any unsaved changes. The new macro avoids that prompt by saving changes automatically.

Note that you could remove the FileClose instruction from the macro if you wanted to:


Sub MAIN
    MsgBox "Sorry, the Close command is disabled."
End Sub

Now, when you choose the Close command, Word does not close the document window, but displays the following message box.

In fact, you could remove every instruction from the macro, so that only Sub MAIN and End Sub remain:


Sub MAIN
End Sub

Now, when you choose the Close command, nothing happens at all. In this way, you can not only modify Word commands but disable them. Disabling commands can be useful if you are creating an environment for a particular kind of document and don't want the user to be able to run certain commands. (Note that you can also disable a Word command by removing it from its menu, toolbar, and shortcut key assignments.)

If you want to run a modified command in another macro, you use a ToolsMacro instruction to run the macro that replaces the original command. For example, to run the FileClose macro, you would use the following instruction:


ToolsMacro .Name = "FileClose", .Run

Note that you could not include this instruction inside the FileClose macro, because Word does not allow a macro to run itself.

Modifying Commands that Display Dialog Boxes

To modify commands that display dialog boxes (the Open command on the File menu, for example), it's helpful to understand how to work with dialog records. For information on dialog records and modifying commands that display dialog boxes, see "Working with Dialog Records" in Chapter 4, "Advanced WordBasic."


Restoring a Modified Command

Since modifying a Word command involves creating a macro with the same name as the command you want to modify, globally restoring an original Word command is simply a matter of deleting or renaming the macro that replaced it in the active or Normal template.

In other cases, you might want to modify a command globally, but restore the original command for a particular template. For example, you could modify the Open command on the File menu (FileOpen) so that it lists every file in a folder instead of just the Word document files. But perhaps a particular template only uses Word document files. It would make sense to restore the original Open command for that template.

To restore an original Word command only for a particular template, you create a second replacement macro that overrides the replacement macro stored in the Normal template. This second replacement macro just runs the original Word command. To create this replacement macro, you follow the same steps used to create the first replacement macro: specify the command you want to restore from the list of Word commands, select the template name in the Macros Available In box, and then choose the Create button to create the new macro.

The trick to this procedure is that even though you don't want to modify the command, you must still make some change to the second replacement macro. Otherwise, Word will not save it when you close it. You can simply add a space to the command and delete the space. This action "dirties" the macro so that Word will save it when you choose the Save Template command or close the macro-editing window. The requirement that you change the macro in some way is a safeguard to prevent you from opening a macro and replacing a Word command without intending to.