Creating Keyboard Shortcuts with Macros in Microsoft Word

in vba •  7 years ago 

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.

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

We all use macros, its very helpful, thanks for sharing.

Using these short keys one can save tons of time and produce heavy productivity in any organization. Thanks for sharing very useful stuff @ghasemkiani.

@ghasemkiani, Thanks for sharing very useful stuff and by Using these short keys anyone can save much time and produce heavy productivity in any organization.

Ah, where is the time i played around with virtual basic and coded my first calculator.

thanks for shairing

wellcome friend

very important post all microsoft user...
thnx for share this post

This post has received gratitude of 9.88 % from @appreciator thanks to: @ghasemkiani.

  ·  7 years ago (edited)

Gd post dear @ghasemkiani & thanx you so much for sharing . & #resteem & upvote done .

Hey dear @ghasemkiani really thanx you so much for sharing this helpful microsoft-word comand sharing .

nice post. Shared :)