Hack 61 Intercept Word CommandsThis hack shows you how to change the way Word works with the ominous-sounding technique of command interception. What happens when you choose Save from the File menu? Obviously, Word saves the current file. But that's not the whole story. You've executed the FileSave command, which tells Word to do the actual work of writing the file to disk on your computer. Likewise, when someone dials your phone number, a computer somewhere inside the phone company directs the call to the phone line in your house. If you move, you can just ask the phone company to send calls to your new house instead. You can even temporarily forward your calls just about anywhere, and the people dialing your number will be none the wiser. Word lets you do the same sort of thing with its built-in commands (such as FileSave). The concept of intercepting commands has been around for a long time, but Word has made it very easy to doand, perhaps more importantly, very easy to undo.
To run the sample macros in this section, place them in the template of your choice [Hack #50] . They will run when you execute the commands after which they're named. To continue with the telephone analogy, this example shows you how to forward calls made to FileSave: Sub FileSave( ) MsgBox "You have executed the FileSave command!" End Sub Go ahead, try and save the file. You'll get the dialog shown in Figure 7-4. Figure 7-4. A simple example of an intercepted commandIn this example, the intercepted command does not save the file, nor will it unless you include some additional code in your macro. To actually save the file, change the macro as follows: Sub FileSave( ) ActiveDocument.Save MsgBox "You've saved the file." End Sub This may seem like a trivial example, but it shows how easily you can change Word's behavior. 7.6.1 Finding the Command's Name
To intercept a command, you need to know its name. For some commands, like Bold, it's pretty easy. Others, like MailMergeAskToConvertChevrons, aren't so straightforward. Fortunately, you can figure out the exact name of a command in several ways. If you have a general idea of the command name, or if you just want
to browse the available commands, select
Tools Figure 7-5. A description of each command is also provided (even if it is grayed out)Select the command you want to intercept, choose the template or document where you want to create the macro from the "Macros in" drop-down menu, and click the Create button to display the Visual Basic equivalent of the command you selected, as shown in Figure 7-6. You can then replace the VBA code with your own code. Figure 7-6. Word launches the Visual Basic Editor and fills in the VBA version of the command you selectedIf you can find the command within the Word interface, press Ctrl-Alt and the "+" key on the number pad. Your cursor will turn into a cloverleaf (just like the Command symbol on a Macintosh). If you click any button or menu item, it will bring up the Customize Keyboard dialog shown in Figure 7-7, which will also tell you the name of the particular command. Figure 7-7. This dialog will tell you the name of a commandTo get a list of all available commands, choose the ListCommands macro from the list of Word commands shown in Figure 7-5 and click the Run button to create a new document with a table listing each Word command. You can also find a more useful and detailed list of commands at http://www.word.mvps.org/faqs/general/CommandsList. 7.6.2 Command Precedence
What happens if an intercept macro in a document shares the same name as one in the Normal template? When you execute a command, Word first searches the document for any macros with the same name as the command, then it searches the document template, then it searches the installed global templates. If it finds a macro with the same name as the command, Word runs the macro instead of the command, and then stops looking. 7.6.3 See Also
|