You want to run a Word macro when you right-click while holding a modifier key such as Ctrl, Shift, or Alt. Word does not natively support assigning macros to right-click context menus with modifier keys. This article explains how to use the OnKey method with a custom keyboard shortcut that mimics the behavior of a right-click plus modifier combination. You will learn to assign a macro to a keystroke like Ctrl+Shift+F10 and then trigger it by right-clicking while holding the modifier keys.
Key Takeaways: Triggering a Word Macro With a Right-Click Plus Modifier
- Word VBA OnKey method with a custom key combination: Assigns a macro to a keystroke like Ctrl+Shift+F10 that you press while right-clicking
- Developer tab > Visual Basic > Insert Module: Where you write the macro code that runs when the key combination is pressed
- Customize Ribbon > Developer tab: Must be enabled before you can access the VBA editor and macro tools
The OnKey Method and Modifier Key Combinations in Word
Word’s macro security model does not allow macros to intercept the right-click event directly. The right-click event is reserved for the built-in context menu. However, you can create a macro that runs when you press a keyboard shortcut that involves the right mouse button as a modifier. The OnKey method in Word VBA lets you assign a macro to any key or key combination, including keys that include the right-click button. The right-click button is represented as the string “+{RButton}” in the OnKey syntax. By combining a modifier key such as Ctrl or Shift with the right-click button, you create a trigger that activates only when you right-click while holding that modifier.
Before you begin, ensure that the Developer tab is visible in the Word ribbon. Go to File > Options > Customize Ribbon. In the right panel, check the box next to Developer and click OK. You also need to have basic familiarity with the Visual Basic for Applications editor, which you open by clicking the Visual Basic button on the Developer tab.
Steps to Create a Macro Triggered by Right-Click Plus a Modifier
- Open the VBA editor and insert a module
On the Developer tab, click Visual Basic. In the Project Explorer pane, expand Normal or your document name. Right-click anywhere in the tree and choose Insert > Module. A blank code window appears. - Write the macro that will run on the trigger
In the module, type a Sub procedure. For example:Sub MyRightClickMacro()
MsgBox "You right-clicked while holding Ctrl"
End Sub
Replace the MsgBox line with your actual macro actions. Close the VBA editor when done. - Assign the macro to a right-click plus modifier key combination
In the VBA editor, open the Immediate Window by pressing Ctrl+G. Type the following line and press Enter:Application.OnKey "^+{RButton}", "MyRightClickMacro"
The caret ^ represents Ctrl. The plus sign + represents Shift. The string “{RButton}” is the right mouse button. This line assigns the macro to Ctrl+Shift+Right-Click. To use only Ctrl and right-click, type"^{RButton}". To use only Shift and right-click, type"+{RButton}". - Test the trigger
Close the VBA editor. Press and hold Ctrl and Shift together. While holding them, right-click anywhere in the Word document. The macro runs immediately. If nothing happens, verify that the Immediate Window command executed without error and that the macro name is spelled correctly. - Make the assignment permanent by adding it to the document or template
The OnKey assignment from the Immediate Window lasts only until you close Word. To make it permanent, add the OnKey line to the ThisDocument module’s Document_Open event or to the Normal template’s AutoExec macro. For example, in the ThisDocument module, select Document from the left dropdown and Open from the right dropdown, then insert:Private Sub Document_Open()
Application.OnKey "^+{RButton}", "MyRightClickMacro"
End Sub - Remove the assignment when no longer needed
To unassign the key combination, run this line in the Immediate Window or in a macro:Application.OnKey "^+{RButton}"
Omitting the macro name argument restores the default behavior for that key combination.
Common Problems When Using a Right-Click Modifier Macro
Macro does not run when I right-click while holding the modifier keys
The most common cause is that the OnKey assignment was not executed. Verify that you typed the OnKey command in the Immediate Window exactly as shown, including the quotation marks. Also confirm that the macro name matches the Sub name in your module. If you are using a document-level assignment, ensure the document’s macro security setting is not blocking macros. Go to File > Options > Trust Center > Trust Center Settings > Macro Settings and select Enable all macros if you are testing. Remember to disable this after testing for security reasons.
The right-click context menu still appears when I use the modifier combination
Word displays the context menu after the macro runs. The OnKey method intercepts the key combination but does not suppress the default right-click action. To suppress the context menu, add this line at the beginning of your macro: Application.CommandBars("Context Menu").Enabled = False. At the end of the macro, re-enable it: Application.CommandBars("Context Menu").Enabled = True. Be aware that disabling the context menu globally may affect other right-click operations while the macro runs.
The macro runs on every right-click even without the modifier key
This happens if you assigned the macro to the plain right-click key without a modifier. Check your OnKey command. The string for a plain right-click is just “{RButton}”. If you used that, the macro runs on every right-click. Change the command to include a modifier such as “^{RButton}” for Ctrl+Right-Click or “+{RButton}” for Shift+Right-Click.
Comparison of Trigger Methods for Word Macros
| Item | OnKey With Right-Click Modifier | Standard Keyboard Shortcut |
|---|---|---|
| Trigger action | Right-click while holding Ctrl, Shift, or Alt | Keyboard keys only, no mouse involvement |
| Ease of setup | Requires VBA code in Immediate Window or module | Can be set via File > Options > Customize Ribbon > Customize |
| Context menu behavior | Context menu appears after macro runs unless suppressed in code | No context menu interference |
| Persistence across sessions | Must be assigned in Document_Open or AutoExec macro | Persists automatically once saved in Normal.dotm |
| Security | Requires macros enabled; potential for accidental trigger | Same macro security requirements |
Now you can create a Word macro that responds to a right-click while you hold a modifier key. Use the OnKey method with the {RButton} string and your chosen modifier symbols. Start by testing with a simple MsgBox macro to confirm the trigger works. For a more advanced setup, suppress the context menu inside the macro to create a clean custom right-click action.