Overriding the Built-In Menus and Commands in Microsoft Word
Summary: Discover how to override Microsoft Word built-in menus and commands. (4 printed pages)
Creating Subroutines to Override Menus
Locating Menu Commands
When You Can't Locate a Command Name
Overriding Toolbar Buttons and Keyboard Shortcuts
What about Other Office Applications?
A simple way to customize the way Microsoft® Word interacts with templates and add-ins that you develop is to write procedures that override built-in menus and commands. For example, perhaps you want verify that a user has filled out the Properties dialog box every time a user saves a document, and display the dialog box if the necessary fields are blank. Or maybe you want to save documents to a document library on a server every time a user saves a document. You can do these things and more by creating some fairly simple routines.
Often, you can override menu commands by creating a subroutine named after the menu and the menu command. For example, if you want to create a subroutine that displays a custom font dialog box instead of the built-in Font dialog box, you would create a subroutine called FormatFont and place the call to display your custom dialog box inside the subroutine. For example, the following subroutine displays a message every time the user clicks the Font command on the Format menu.
Sub FormatFont() MsgBox "FormatFont" End Sub
While you can use the above method to override most menu commands, there are some menu commands for which this doesn't work. In these cases, you would need to locate a different command name.
Some of the menu and toolbar button commands aren't easily discoverable, which means you might have to work to locate them. In some cases, the name of the menu command is the same as the name of the subroutine, without prefacing the name of the subroutine with the name of the top-level menu. For example, to create a subroutine to override the Web Page Preview option on the File menu, you would need to create a subroutine named WebPagePreview instead of FileWebPagePreview.
In other cases, the name of the command corresponds to a different top-level menu and not the menu where it currently exists. Perhaps this is because the option at one time existed on a different menu. For example, the Templates and Add-ins option is on the Tools menu. However, to override it, you would need to create a subroutine named FileTemplates rather than ToolsTemplatesAndAddIns.
Woody Leonhard (author of the Hacker's Guide to Word and Woody's Office Watch, among other titles) told me about me a little-known trick that you can use to locate command names for menu commands and toolbar buttons, which he documents in the Hacker's Guide to Word. Although he wrote the book for Microsoft Word 2.0, this trick works just as well in Microsoft Word 2002.
To determine the command names for most menu commands, hold down CTRL+ALT and then press the PLUS key on the numeric keypad. The mouse pointer changes to . Then click the mouse on any menu item or toolbar button for which you want the command. The Customize Keyboard dialog box displays with the name of the related command in the Commands list. (If the Customize Keyboard dialog box doesn't display, you can assume there isn't a corresponding command that you can use to override the menu option or toolbar button.)
In some rare instances, this trick doesn't return a command name that works. For instance, when I clicked Diagram on the Insert menu, the Customize Keyboard dialog box offered a command called DrawInsertDiagram, but when I created a similarly named subroutine, it failed to override the Diagram command on the Insert menu. After playing around with names like InsertDrawInsertDiagram and InsertDrawDiagram, I was still unable to locate a viable command name, so I had to use other means to override this menu option.
If you are unable to locate a command name to override a menu command or a toolbar button, as I was unable to locate the Diagram command on the Insert menu, you can use the Microsoft Office CommandBar object to override the built-in commands.
To do this, first create a custom subroutine, giving it any name you want. For example, to override the Diagram command on the Insert menu, I created the following subroutine. However, by itself, this subroutine doesn't override the option.
Sub OverrideInsertDiagram() MsgBox "OverrideInsertDiagram" End Sub
Next, create a subroutine to specify the menu command to override. In this subroutine, specify the CommandBar and CommandBarControl that you want to override. Then, using the OnAction property of the CommandBarControl object, specify the name of the subroutine that you created above. The following example demonstrates this.
Sub OverrideDiagramMenuCommand() Application.CustomizationContext = ActiveDocument.AttachedTemplate CommandBars("Insert").Controls("Dia&gram...") _ .OnAction = "OverrideInsertDiagram" End Sub
You should note that when specifying the CommandBarControl object, you must type the menu command name exactly as it appears in the menu, even including any ellipses. Without the ellipses, assigning the menu command to the CommandBarControl object will raise a run-time error (including the ampersand that specifies the letter in the option name that is underlined is optional).
The CustomizationContext property above specifies the template or document in which the customized menu override is applicable. The above code specifies the AttachedTemplate as the template to which to limit the customization. If this code is in a template on which the active document is based, this customization is limited to only documents based on that template.
In addition to overriding menu commands, you can also override many of the toolbar buttons and keyboard shortcuts. In fact, when you use a built-in command to override a menu item, the subroutine also overrides associated toolbar buttons and keyboard shortcuts. However, there are many toolbar buttons that are not directly associated with a menu command. Therefore, you can locate the associated toolbar button or keyboard shortcut command from the list of built-in commands located in the Macros dialog box. Just display the dialog box (click Tools, point to Macro, and then click Macros) and select Word Commands in the Macros in dropdown list.
You can also get a list of the built-in commands using the ListCommands method of the Application object. The ListCommands method creates a new document that contains a table of all the commands with their associated menu or keyboard shortcut (note that this list is not necessarily comprehensive). To create this list, run the following subroutine.
Sub GetBuiltInCommands() Application.ListCommands True End Sub
After you have the list of commands, you can override most of the commands by removing any spaces from the command name, and then creating a subroutine named after the command name. For example, if you create a subroutine called Bold, you will override the Bold button on the Formatting toolbar and the CTRL+B keyboard shortcut.
Although Word is the only application that provides direct support for overriding built-in menu, toolbar, and keyboard commands, you can use the CommandBar objects to override built-in menus in other Office applications. Therefore, if you are in Microsoft Excel, Microsoft Access, Microsoft PowerPoint®, or Microsoft FrontPage® and want to create a subroutine to override any of the menus, you can you use the CommandBar objects as shown above to override any (or all) of the menu items.
The download included with this article contains a template called override.dot that you can use to locate most (if not all) of the menu commands as well as many of the toolbar button and keyboard commands. Combined with the tips included in this article you should be on your way to creating templates and add-ins that can do many different things.