Word

Add Keybinding to document to launch Macro

Ease of Use

Easy

Version tested with

2000, 2002 

Submitted by:

MOS MASTER

Description:

This code will add a keybinding (Alt+0) to the document. If the shortcut Alt+0 is pressed the macro will execute. 

Discussion:

Sometimes you're looking for a quick way to launch a macro. A keybinding is a easy method and you only have to press a key combination to launch the macro. 

Code:

instructions for use

			

' \\ Code for Module1 Option Explicit Sub AddKeyBinding() With Application ' \\ Do customization in THIS document .CustomizationContext = ThisDocument ' \\ Add keybinding to this document Shorcut: Alt+0 .KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyAlt, wdKey0), _ KeyCategory:=wdKeyCategoryCommand, _ Command:="TestKeybinding" End With End Sub ' \\ Code for Module2 Option Explicit ' \\ Test sub for keybinding Sub TestKeybinding() MsgBox "We have a winner", vbInformation, "Succes" End Sub

How to use:

  1. Open your Word document.
  2. Press Alt + F11 to open VBE.
  3. Insert-Module. (Insert -> module)
  4. Paste the code for Module1 in the window at right. (F7)
  5. Insert-Module. (Insert -> module)
  6. Paste the code for Module2 in the window at right. (F7)
  7. Close VBE (Alt + Q or press the X in the top right hand corner).
  8. Save the file.
 

Test the code:

  1. From Word, press Alt + F8 to open the macro dialog box.
  2. Select AddKeyBinding
  3. Click Run.
  4. Press shortcut Alt + 0 and the msgbox will show from sub: "TestKeybinding"
  5. Remember to save the document if you want to save the keybinding.
 

Sample File:

Add Keycode.zip 7.42KB 

Approved by mdmackillop


This entry has been viewed 79 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2014 VBA Express