Hack 61 Intercept Word Commands![]() ![]() 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.
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( )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 command![]() will it unless you include some additional code in your macro. To actually save the file, change the macro as follows: Sub FileSave( )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 NameTo 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 the "Macros in" pull-down menu, as shown in Figure 7-5. Figure 7-5. A description of each command is also provided (even if it is grayed out)![]() 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 selected![]() 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 command![]() 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 PrecedenceWhat 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[Hack #35] [Hack #39] [Hack #42] |