Where can I find keyboard constants that work in VBA for MS Word?

    I am writing VBA code in a Word document running Word 2007 in Windows 7.
    I would like to let a user use a keyboard shortcut to run a macro named “Exercice1” by using:
                     Shift + Alt + 1 on the numeric keypad.
    The usual way to assign a keyboard shortcut in VBA is with this kind of code:

           KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, _
               Command:="Exercice1", _
               KeyCode:=BuildKeyCode(wdKeyShift, wdKeyAlt, wdKey1)

    The second line of that code names the macro and the last line assigns the shortcut key combination.

    My problem is what word to use to indicate number 1 on the numeric keypad. The code above successfully assigns the combination Shift + Alt + 1 (at the top of the keyboard) to the “Exercice1” macro.
    I have attached a list of keyboard constants that usually work, but not always. It says that the constant for the numeric keypad 1 is “wdKeyNumeric1”. That does not work in the above code.

    If I assign a keyboard shortcut manually using OfficeKey > Word Options > Customize > Customize Keyboard Shortcuts > Macros etc. and press Shift + Alt + 1 on the numeric keyboard, it assigns the shortcut to
           “Alt + End”.
    That works, but those are not the VBA code words to use.

    “wdKeyEnd” also does not work.

     I guess my chart of keyboard constants does not work, but where can I find a better chart of keyboard constants?

      Thanks for any help!
      John Robin (Allen)

JohnRobinAllenRetired professor of FrenchAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Travis HydzikVariousCommented:
wdKeyNumeric1 should have worked,

a list on the microsoft msdn can be found here
JohnRobinAllenRetired professor of FrenchAuthor Commented:
I agree. It should have worked. However, it does not work. Not on my machine. How about on your machine?
Travis HydzikVariousCommented:
yeh, it doesn't work for me too, strange. Sorry I can't further help.
Become a Microsoft Certified Solutions Expert

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

The problem is that the NumLock feature changes the keystrokes you need. The attached code should work either way.

Sub ShortcutTest()
    Const myMacroName = "Normal.NewMacros.test"
    'First, remove any existing keybindings for this macro
    Dim myKeyBinding As KeyBinding
    For Each myKeyBinding In KeyBindings
        If myKeyBinding.Command = myMacroName Then
        End If
    Next myKeyBinding
    'Add keybindings (one works with NumLock on, the other with NumLock off)
    KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, _
        Command:=myMacroName, _
        KeyCode:=BuildKeyCode(wdKeyShift, wdKeyAlt, wdKeyEnd)
    KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, _
        Command:=myMacroName, _
        KeyCode:=BuildKeyCode(wdKeyAlt, wdKeyEnd)
End Sub

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JohnRobinAllenRetired professor of FrenchAuthor Commented:
At first glance, I cannot make the code above work. I am sure it is my  fault.
I first do the equivalent of 2 through 9 in the code sample above, only I clear all macro shortcuts in the active document:
Dim itm As Integer
       CustomizationContext = ActiveDocument

      For itm = KeyBindings.Count To 1 Step -1

Then I add shortcuts the usual way. Instead of loading the name of the macro into the Const myMacroName, I put it into each 3-line command. First I do the shortcuts that work:

      KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, _
      Command:="Exercice1", _
      KeyCode:=BuildKeyCode(wdKeyShift, wdKeyAlt, wdKey1)

      KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, _
      Command:="Exercice1", _
      KeyCode:=BuildKeyCode(wdKeyShift, wdKeyAlt, wdKeyF1)

Then I try to put in the numeric pad shortcut, which appear to be almost the same keyboard shortcuts, one with wdKeyShift and one without that, as you can see in lines 13 and 15 in the code sample above.

      KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, _
      Command:="Exercice1", _
      KeyCode:=BuildKeyCode(wdKeyShift, wdKeyAlt, wdKeyEnd)

      KeyBindings.Add KeyCategory:=wdKeyCategoryMacro, _
      Command:="Exercice1", _
      KeyCode:=BuildKeyCode(wdKeyAlt, wdKeyEnd)

The result is no change from before. If you use OfficeKey > Word Options > Customize >  Customize Keyboard Shortcuts > Macros to look at the macros loaded, it shows that the shortcut key sequence is apparently loaded. (See attached image). However, the key does not work.

 The only difference I can see between my code and the code in the sample above is that I spell out the name "Exercice1" whereas the sample code puts "Exercice1" into the constant myMacroName.

I keep thinking there must be some simple but stupid mistake I am making, but I do not know where it is.


Best to start with something that works. What happens when you try my macro exactly as written?
JohnRobinAllenRetired professor of FrenchAuthor Commented:
I tried the code exactly as written by lweber and it works. I am, therefore, awarding the points with gratitude, but there must be more to the story. I know my version of the code did not work. Now I have to find out what it is in my code that prevented the macro from working.
Originally I thought it might be the other keyboard sequences I assigned to the same macro, so I added them to the code that I just tried out. The code still works. Perhaps it is because I did not use a Const for the macro name but, instead, used a plain string.
If I find out why my previous code did not work, if it is something other than a typo on my part, I will post the results here.

Thanks for the help.
john robin (allen)
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.