Using F1 and Shift F1 in Word Macros

T

Tom Hall

Some time ago I asked for help in defining a Word macro that could be bound
to the F1 key. Someone came up with the following, which worked quite
nicely.

Sub MakeF1()
'
' MakeF1 Macro
' Macro recorded 9/26/2006 by Tom Hall
'
Application.CustomizationContext = ActiveDocument
Application.KeyBindings.Add wdKeyCategoryMacro, "Interviewer", wdKeyF1
End Sub


I have a macro called "Interviewer" which actually transmits the keystrokes
I want the F1 key to activate. As I said, this all works quite well.

Today I discovered that there's another key that Word apparently doesn't
want you to use to define a macro, and that's Shift F1.

I've tried various permutations on the above macro to get the same thing to
happen when I press Shift-F1, but so far to no avail.

I'm assuming that I can do this by replacing "wdKeyF1" with the appropriate
statement for Shift-F1, but so far I haven't been able to find one that
works.

I would of course expect to replace "Interviewer" with another macro
separately defined, followed by the correct designation for Shift-F1, but
so far I haven't found it.

Can the same workaround that worked for F1 be made to work for Shift-F1?


Tom
 
K

Klaus Linke

Hi Tom,

If you want to assign shortcuts involving more than one key, you can use
BuildKeyCode:

Application.CustomizationContext = ActiveDocument
Application.KeyBindings.Add _
KeyCategory:=wdKeyCategoryMacro, _
Command:="Interviewer", _
KeyCode:=BuildKeyCode(wdKeyShift, wdKeyF1)

Regards,
Klaus
 
T

Tom Hall

Hi Tom,

If you want to assign shortcuts involving more than one key, you can use
BuildKeyCode:

Application.CustomizationContext = ActiveDocument
Application.KeyBindings.Add _
KeyCategory:=wdKeyCategoryMacro, _
Command:="Interviewer", _
KeyCode:=BuildKeyCode(wdKeyShift, wdKeyF1)

Thank you! This is exactly what I was looking for!


Tom
 
G

Greg_W

Hey all,

I am trying to do the same thing, but keep coming up with an error message.
What i want to do is programmatically assign a keyboard shortcut key
combination to a macro. The command for the macro is
"MyMacros.basSpanishAccents.UpsideDownExclamation".

Here's the code giving me trouble:

For Each ctl In tbr.Controls
strCommand = ctl.Caption
If ctl.Caption = "MyMacros.basSpanishAccents.UpsideDownExclamation" Then
KeyBindings("MyMacros.basSpanishAccents.UpsidedownExclamation").Clear
KeyBindings.Add _
KeyCode:=BuildKeyCode(wdKeyF10, wdKey1), _
KeyCategory:=wdKeyCategoryCommand, _
Command:=strCommand
End If
Next

For some reason, it's not accepting the key combination F11 + F1. I get a
message saying "Invalid parameter." Run-Time error 5853.

Anyone know what I'm doing wrong?
 
K

Klaus Linke

Hi Greg,

Not sure, but I think there are just some limitations to "overloading" F10
(Menu Mode) and F1 (Help)... Probably a Windows limitation rather than one
in Word. Unfortunately I haven't ever come across a good reference to what
those limitations are.
I'd be interested in such a reference myself ... Maybe someone can help both
of us out?

As soon as you use F10, the focus will go to the menu. If you'd redefine it
as a prefix key, this functionality would be lost. So Windows keeps you from
messing with its functionality.

Not sure why F1 can't be the second key in "F11, F1"... but likely also a
Windows limitation.


Regards,
Klaus
 
G

Greg_W

Klaus,

Thanks for the response. Do you have a list, or know where I can find a
list, of the combinations that will work?
 
T

Tony Jollans

I'm unclear as to exactly what key combination(s) you are trying to use but
all you can do is use one key, and up to three modifier keys. The modifier
keys are Shift, Ctrl, and Alt. The F keys are just keys - not modifier keys.

Word is just behaving as any other application does - as any other
application must do. The keyboard will not send F10 and something else so it
can't be used.
 
K

Klaus Linke

Tony Jollans said:
I'm unclear as to exactly what key combination(s) you are trying to use
but all you can do is use one key, and up to three modifier keys. The
modifier keys are Shift, Ctrl, and Alt. The F keys are just keys - not
modifier keys.

Some of them can be used as prefix keys though, which is what Greg was
trying to do.
Say like F11,A = hit F11, release, hit A.

I use F2 as a prefix keys for most of my custom shortcuts since there are
already so many predefined shortcuts involving Shift, Alt, Ctrl and
combinations thereof, and with the function key prefix F2 I know I can
follow that by any key.

Regards,
Klaus
 
K

Klaus Linke

Do you have a list, or know where I can find a
list, of the combinations that will work?

As I said, no, I don't know a reference for that... sorry.

Klaus
 
T

Tony Jollans

I didn't understand that from his post, and his code does not try to do
that. As far as I know there are no particular restrictions, except that
some combinations can not be entered through the UI.

To assign F10 followed by F1, change ..

KeyCode:=BuildKeyCode(wdKeyF10, wdKey1), _

.. to ..

KeyCode:=BuildKeyCode(wdKeyF10), _
KeyCode2:=BuildKeyCode(wdKeyF1), _
 
G

Greg_W

Klaus and Tony,

Maybe I need to back up a bit and explain what I'm trying to do. I work in
an environment where we do a lot of translation into Spanish. I have created
a toolbar called "Spanish Accents" that enables me to create the accented
Spanish letters as well as the ñ, ü, and other diacritic characters.

After creating the toolbar, I customized it by opening the Customize dialog,
clicking the Command tab, scrolling down to Macros, selecting the macro for
which I want to set a shortcut combination, clicking the Keyboard button, and
then specifying the keyboard sequence there. IT WORKS fine that way. I can
use F10,1; F11A; or whatever I want.

So, now I want to share this toolbar with all other personnel in our office.
I want to create a setup routine that makes sure that these key combination
assignments are in place. So, I'm using VBA to try to accomplish the same
thing I did through the Customize dialog. That is where it does not accept
certain key combinations, the same ones that are accepted when i do it
manually.

Does this makes sense?

So, why is it that I can use F10 + 1 when I set it up through the customize
dialog, but cannot do it using VBA?
 
T

Tony Jollans

It makes total sense, Greg. What you need to do is as I said in my last
post.

BuildKeyCode(wdKeyF10, wdKey1) tries to build a code for a single
combination of keys F10 and 1

To use one key followed by another (like F10;1 in the UI) you must code them
as separate parameters, keycode and keycode2:

KeyCode:=BuildKeyCode(wdKeyF10), _
KeyCode2:=BuildKeyCode(wdKey1), _
 

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