Heading

This is some text inside of a div block.
This is some text inside of a div block.
This is some text inside of a div block.
min read

Creating Keyboard Shortcuts in a Macro

Word • Macros
Peter Ronhovde
18
min read

As soon as you get more than a few custom editing macros, you’ll be clamoring for an automated way to set your keyboard shortcuts, so you're welcome!

Thanks for your interest

This content is part of a paid plan.

Manually add a keyboard shortcut

We can manually add keyboard shortcuts through the Word interface, but the method depends on the platform.

The process is a little cumbersome in Windows, but this video covers the steps. For Mac, you access it from the Tools menu (see the introductory portion of this article).

On Macs the manual approach is the only way to add keyboard shortcuts with all four modifier keys (Command+Control+Option+Shift+Key). I wish Word for Windows would allow us access to the Windows command key. The extra modifier key on Mac is nice when setting up keyboard shortcuts (although they don't support other keys the same way).

But it’s tedious …

Manual works, but it’s tedious for anything more than a few customized keyboard shortcuts.

Why?

Sometimes Word will spontaneously reset some or all your custom keyboard shortcuts. For example, this can happen if the Normal.dot template is corrupted and regenerated by Word. Also, the VBA editor will cancel a custom shortcut if you happen to copy or create another macro with the same name within the editor since it doesn't know which macro to use.

Having a way to quickly reset all my keyboard shortcuts is not just wonderful; it’s almost essential to me if I plan on using them regularly.

Some common language

“Key binding” is connecting a particular key combination to an action of some kind.

A “modifier key” is one of the special keys we use for keyboard shortcuts.

Possible key combinations

On Windows, we can use modifier keys: Control, Alt, and/or Shift keys to combine with most other keys. The Windows key is special for the OS. Us mortals aren’t allowed to use it in Word (I want to change this! Contact me Microsoft, so I can change your mind).

On Macs, we can use modifier keys: Command, Control, Option, and/or Shift.

Problem key combinations

On both Mac and Windows, there are some limitations on which key combinations work. If you’re unsure, you can test unusual combinations by using the manual approach mentioned above.

Windows tends to fuss more about the Alt key being used with some combinations. Word for Mac seems to dislike combining the Shift and maybe the option keys with some keys especially punctuation marks.

At most three modifier keys

We can have at most three modifier keys in Word for Windows or Mac when setting it up in a macro, but in Word for Mac, you can use the manual method at the top to add keyboard shortcuts that use all four modifier keys.

Add a keyboard shortcut for a macro

Our goal is to automatically bind a keyboard shortcut of our choice to a macro.

Break it down

The command to add a new key binding command starts easy

KeyBindings.Add

We then need to specify the category, command, and the key code corresponding to the keyboard shortcut.

Key Category

KeyCategory is the type of command or action being mapped to a key combination. Despite the numbers listed in the documentation, the Mac values seem to be different for at least some modifier keys.

The main categories applicable for us are:

  • wdKeyCategoryMacro – Assign the upcoming keyboard shortcut to one of your own macros
  • wdKeyCategoryCommand – Assign the upcoming keyboard shortcut to one of Microsoft’s standard Word commands (the list is found in the manual entry dialog box mentioned up top)

Others you might use at some point in the future are:

  • wdKeyCategoryDisable – if you want to disable a keyboard shortcut
  • wdKeyCategoryAutoText – if you want to assign a keyboard shortcut to an AutoText entry (my main use case would be scene setting boilerplate text)

Command

Which command or macro are we assigning to the keyboard shortcut?

For both macros and Word commands, it’s just the name inside double quotes. Unfortunately, finding the name of a particular Word command you want to reassign can be a little challenging in some cases since they are abbreviated names (when searching manually above) and documentation is difficult to find.

Keyboard shortcut

The KeyCode is a number that acts as a unique key combination identifier. This is slightly more complicated, but they give us a way to “build” a key code that represents the keyboard shortcut.

Basically, pack your chosen modifier keys and the target key into the BuildKeyCode function below with up to four total keys. Assign this value to the KeyCode option in the key binding add method above.

BuildKeyCode function

The function looks like:

BuildKeyCode(RequiredKey1, OptionalKey2, OptionalKey3, OptionalKey4)

See this key list, but most of them just have the pattern wdKey followed by a letter, number, or relatively obvious key description.

If you’re using a regular key (letters, number, some punctuation marks, etc.), you must also have at least one modifier key. The exception is if you’re mapping to a function key.

On Windows

For Windows, the key values corresponding to the modifier keys are: wdKeyControl, wdKeyAlt, and wdKeyShift

On Mac

For Mac, the key values corresponding to modifier keys are: wdKeyCommand, wdKeyControl, wdKeyOption, and wdKeyShift.

No more than four keys are allowed in the BuildKeyCode function, including the trigger key, which is why we can’t automatically assign Command+Control+Option+Shift with some other key on a Mac. That would be five total keys, but you can still assign it manually which I’ve done for a few macros.

BuildKeyCode details

The BuildKeyCode function seems to just add the numerical key values together along with performing some validation checks before it makes the keyboard shortcut assignment. There is a pattern to the key values to make this work under the hood, but that is set by Microsoft Word, so we don’t have to worry about them.

Finally assign your macro keyboard shortcut

A typical key binding command might look like

KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, Command:="MyMacroName", _
KeyCode:=BuildKeyCode(wdKeyControl, wdKeyAlt, wdKeyM)

Well, that’s a doozy.

Don’t forget the underscore _ character at the end of the first line allows us to continue a long VBA line on the next text editor line.

This key binding command would add the Word keyboard shortcut Control+Alt+M on a Windows system to a macro called MyMacroName.

I’ve also used it to reassign some of the standard Word commands (see below). For example, I’ve grown accustomed to the more Mac-like Control+Shift+Z for the Redo/Repeat keyboard shortcut. Another application uses this key combination for Redo, and my muscle memory with the Windows-flavored Control+Y was giving me fits at times.

Create the empty macro

A previous post covers creating an empty macro like the one shown below. When you’re done, you’ll have something like:

Sub AddMyKeyboardShortcuts()
' Add my custom macro key bindings ...

End Sub

The single quote tells VBA the rest of the text on that line is a comment meant for human readers. We start our macro steps on the empty line.

Final Macro

Now create a macro to keep all your keyboard shortcuts together.

Sub AddMyKeyboardShortcuts()
' Add my custom macro key bindings ...
' Assign a fake macro to Control+Alt+M
KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, Command:="MyMacroName", _
KeyCode:=BuildKeyCode(wdKeyControl, wdKeyAlt, wdKeyM)

' Add my Word command key bindings ...
' Assign Word Redo or Repeat command to Control+Shift+Z
KeyBindings.Add KeyCategory:=wdKeyCategoryCommand, Command:="EditRedoOrRepeat", _
KeyCode:=BuildKeyCode(wdKeyControl, wdKeyShift, wdKeyZ)
End Sub

This first key binding command would assign the Word keyboard shortcut Control+Alt+M on a Windows system to a fake macro called MyMacroName.

Assigning a Word command

Notice in the second one we used the category wdKeyCategoryCommand. The Word name for the Redo or Repeat command is EditRedoOrRepeat. This macro binds it to Control+Shift+Z.

Overriding standard Word keyboard shortcuts

You can override existing keyboard shortcuts, which I often do to better remember them. I also reassign the Function keys that I almost never seems to use otherwise. If you change your mind later, you can reverse the changes.

Use groups as your list grows

I group my assignments into categories for headings, sentences, paragraphs, etc.

Similarly, you can organize your keyboard shortcuts. For example, I associate the Option (or Alt) modifier keys to Sentence macros and try to roughly match how Windows organizes their standard keyboard shortcuts. The grouped assignments don't always work out the way I want, but it does help me remember many of them.

But it’s messy ...

Notice how messy the key binds command looks in the macro above, and your list will grow faster than you think as you add macros to your editing toolbox. Upcoming member content will cover some functions to make adding shortcuts easier.

Tips

You can map keyboard shortcuts to Function keys higher than F12. Not all combinations work, but this can allow you to set up some additional triggers if you happen to use macro keys on a fancy keyboard (Logitech has several keyboards like this) or an Elgato Stream Deck.

This is higher level stuff though, so stick with what’s comfortable for you. The whole point is to use it to help you edit faster not get bogged down in the process.

What else can it be used for?

You can also assign keyboard shortcuts to AutoText entries, styles, symbols, and font modifications. My main use case for AutoText entries would be scene setting boilerplate text, but I haven't used these options much.

Let me know if you have an interesting use case.

Two key shortcuts

We can also create two-key shortcuts, like when accessing the Word menu system with the keyboard, by adding a KeyCode2 argument to the key binding command. If you’re interested in this, let me know.

Affiliate Links

If you're interested in using Word or another tool related to the article, check out these affiliate links. I may make a small commission if you purchase when using them, but there is no increase in cost for you, and it helps to support this site and associated content.

I've been using Microsoft for Business for commercial use (that's us writers) on one of the lower pricing tiers for years. I get to use my macros, have online storage, and don't have to worry about software updates.