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)

Keyboard-constants.doc
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.

thydzikCommented:
wdKeyNumeric1 should have worked,

a list on the microsoft msdn can be found here
http://msdn.microsoft.com/en-us/library/aa171575%28office.11%29.aspx
0
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?
      j.r.
0
thydzikCommented:
yeh, it doesn't work for me too, strange. Sorry I can't further help.
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

lwebberCommented:
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
            myKeyBinding.Clear
        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

0

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
            KeyBindings.Item(itm).Clear
      Next

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.

        J.R.





02-05-2010-2-48-22-PM.png
0
lwebberCommented:
Best to start with something that works. What happens when you try my macro exactly as written?
0
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)
0
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.