Word 2007 key bindings - VBA

O

olley

Hi,

I have problem with KeyBindings in Word 2007. I bind diferrent funcitons to
Alt+8 and Alt+* (on numeric pad). It works just fine in Word 2003. However in
Word 2007 it always calls Alt+8 bound function. Seems that Word 2007 cannot
differentiate between wdKey8 and wdKeyNumericMultiply, even though
BuildKeyCode() returns different values for those combinations.

Does anyone know if this is a bug or a feature in new Word 2007? Is there a
workaround for this?

Thanks!

Here is the code sample. In Word 2007 it always call "EghtPressed"

KeyBindings.Add KeyCode:=BuildKeyCode(
arg1:=wdKeyAlt,
Arg2:=wdKey8),
KeyCategory:=wdKeyCategoryCommand,
Command:="EightPressed"

KeyBindings.Add KeyCode:=BuildKeyCode(
arg1:=wdKeyAlt,
Arg2:=wdKeyNumericMultiply),
KeyCategory:=wdKeyCategoryCommand,
Command:="StarPressed"
 
O

olley

Hi Klaus,

Thanks for your response.
Haven't checked it out in Word 2007, but using the Windows API should do the
trick.
See Romke Soldaat's article
http://msdn2.microsoft.com/en-us/library/Aa155749(office.10).aspx
(scroll down to "The Keybinding Mess" and "Get the API to Do the Work VBA
Can't Do")

Unfortunately it didn't do the trick ;) This article (that particular part
you pointed to) explains how to get correct scancode for any keyboard using
VkKeyScan() Win API function. However, in MSDN for this function they say:
"Translations for the numeric keypad (VK_NUMPAD0 through VK_DIVIDE) are
ignored." And indeed it doesn't build scancode for * on numeric keyboard.

The problem here is not getting correct scancode, but rather is how Word
2007 binds keys on numeric keypad (even for well-known scan codes). To me it
seems that Word 2007 cannot handle numeric keypad key bindings correctly.

Thank you,
 
K

Klaus Linke

Yes, seems like it's reserved or something. I can assign
wdKeyNumericMultiply alone, but not with Alt.
Maybe it has something to do with ribbon navigation (using Alt, and then
moving through the tabs and chunks with the direction keys)?

I can assign Alt+wdKeyNumeric8, though it only works if NumLock is turned
on.

Regards,
Klaus
 
O

olley

Klaus Linke said:
Yes, seems like it's reserved or something. I can assign
wdKeyNumericMultiply alone, but not with Alt.
Maybe it has something to do with ribbon navigation (using Alt, and then
moving through the tabs and chunks with the direction keys)?

I ended up doing following. I bind "8" key with Alt using regular call:
KeyBindings.Add
KeyCode:=BuildKeyCode(arg1:=wdKeyAlt, arg2:=wdKey8),
KeyCategory:=wdKeyCategoryCommand,
Command:="Handler8"

And then in Handler8() funciton I check if wdKeyNumericMultiply is still
pressed. For that I use Win32 API GetKeyState() function. If it is pressed
then I call my regular handler for Alt+<Num*> keystroke. There is a potential
danger that by the time I call GetKeyState() the user would already release
Num*. But it seems to be working for me now. Oh, and I do it only if the
current Word version is 2007.

Klaus, thanks for the idea of using Win32 API directly. I would not get to
it without your hint about different API call ;)
 
M

Maurice Pelchat

Use this instead

BuildKeyCode(wdKeyControl, wdKeyNumeric8)

And if your are not using the numeric keypad, use wdKey8

Hope it may help
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top