Modifying Built-in Commands
This content is no longer actively maintained. It is provided as is, for anyone who may still be using these technologies, with no warranties or claims of accuracy with regard to the most recent product version or service release.
One simple but very powerful method you can use to customize the way Microsoft® Word works is to run your own VBA procedure in place of a built-in procedure. Doing this lets you customize the behavior of Word in any way you can imagine.
There is no limit to the kinds of things you can do and the kinds of built-in behaviors you can change. You could save documents created by using your custom template to a different directory than documents created by using Normal.dot. You could modify the File New command to create custom document properties for every new document. You could display your own custom dialog box instead of the Word dialog box normally displayed in response to a menu command. You could also let the built-in command run and then detect whether a user made certain selections from a Word dialog box.
The first thing you have to do is figure out which procedure Word runs to perform a built-in action. This is easy to do for all built-in menu commands. If you press ALT+CTRL+PLUS SIGN (+) on the numeric keypad (not the PLUS SIGN on the keyboard) and then click the menu item you want to investigate, Word displays the Customize Keyboard dialog box, which shows the name of the built-in procedure in the Commands list. For example, in the following figure, you can see that Word runs the FileSaveAs procedure whenever a user clicks Save As on the File menu.
The Customize Keyboard Dialog Box
There are three ways you can substitute your own procedure for a built-in Word procedure:
- In any standard module, create a VBA procedure that uses the same name as the procedure you want to replace. For example, if you create a procedure named FileSaveAs, Word will run your procedure instead of the built-in FileSaveAs procedure whenever the built-in procedure would normally be called.
- Create a module and name it by using the name of the built-in command you want to replace. Then add a subroutine named Main() to the module and add your custom code to that procedure.
- Create a new procedure by using the Macros dialog box. To do this, point to Macro on the Tools menu, and then click Macros. In the Macros dialog box, click Word Commands in the Macros in list. The Macro name list will then display the hundreds of built-in Word procedures. You can learn something about what these procedures do by clicking a procedure name in the list and reading its description in the Description box at the bottom of the dialog box. When you locate the command you want to modify, click it and then use the Macros in list to select the template or document in which to save the procedure. Then click the Create button to create a new VBA procedure that uses the same name as the built-in command.
See Also
Working with Microsoft Word Objects | Understanding Application-Level Objects | Working with the Application Object | Working with the Settings in the Options Dialog Box | Working with Word Dialog Boxes