If you are a power user of Microsoft Word, you probably use keyboard shortcuts frequently. Keyboard shortcuts are one of the features that can increase your productivity to a great extent.
You can assign shortcuts to Word commands, macros, styles, and symbols. Even though this can be done using the Word user interface, sometimes it may be more productive to do this binding programmatically.
Word uses VBA (Visual Basic for Applications) for writing macros. As unfortunate a choice as this seems to be, we have to live with it. Here I will present some hints about assigning keyboard shortcuts using VBA macros. It should be noted that these macros have been tested with Microsoft Word 2007, but they should probably work with later versions, too.
Suppose you want to assign the keyboard shortcut Ctrl-Alt W to the command "Web View". This is how it's done:
KeyBindings.Add KeyCategory:=wdKeyCategoryCommand, Command:="ViewWeb", KeyCode:=BuildKeyCode(wdKeyW, wdKeyAlt, wdKeyControl)
There is also a KeyCode2 parameter that can be used for cases when you want to assign two consecutive keystrokes.
Assigning shortcuts to macros is done in a similar fashion. Suppose we want to assign a shortcut (Alt-Shift S) to a macro called MyMacro:
KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, Command:="MyMacro", KeyCode:=BuildKeyCode(wdKeyS, wdKeyAlt, wdKeyShift)
The same goes for assigning a shortcut to a style. Here is an example, assigning the keyboard shortcut Ctrl-Alt-Shift C to a style called MyStyle:
KeyBindings.Add KeyCategory:=wdKeyCategoryStyle, Command:="MyStyle", KeyCode:=BuildKeyCode(wdKeyC, wdKeyAlt, wdKeyControl, wdKeyShift)
Now, let's assume we want to assign the shortcut Ctrl-Alt-Shift Hyphen to the Unicode character non-breaking hyphen (U+2011). Here is how it is done:
KeyBindings.Add KeyCategory:=wdKeyCategorySymbol, Command:=" " & ChrW(Val("&H2011")), KeyCode:=BuildKeyCode(wdKeyHyphen, wdKeyAlt, wdKeyControl, wdKeyShift)
I just tested this line and it works like a charm. I hope you find this post useful. Of course, if you don't know how to write a macro in Word, that's another story. Here I assumed you have written macros before and you know how to create a macro.