Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / May 2008

Tip: Looking for answers? Try searching our database.

Using F1 and Shift F1 in Word Macros

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Tom Hall - 25 Mar 2008 21:33 GMT
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

Signature

remove .spoo to reply by email

Klaus Linke - 25 Mar 2008 22:30 GMT
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
Tom Hall - 26 Mar 2008 17:52 GMT
>Hi Tom,
>
[quoted text clipped - 6 lines]
>       Command:="Interviewer", _
>       KeyCode:=BuildKeyCode(wdKeyShift, wdKeyF1)

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

Tom

Signature

remove .spoo to reply by email

Greg_W - 26 May 2008 20:43 GMT
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?
Klaus Linke - 27 May 2008 20:27 GMT
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

> Hey all,
>
[quoted text clipped - 23 lines]
>
> Anyone know what I'm doing wrong?
Greg_W - 27 May 2008 23:26 GMT
Klaus,

Thanks for the response. Do you have a list, or know where I can find a
list, of the combinations that will work?
Klaus Linke - 28 May 2008 11:56 GMT
> 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
Tony Jollans - 28 May 2008 10:55 GMT
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.

Signature

Enjoy,
Tony

> Hey all,
>
[quoted text clipped - 23 lines]
>
> Anyone know what I'm doing wrong?
Klaus Linke - 28 May 2008 11:55 GMT
> 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
Tony Jollans - 28 May 2008 12:15 GMT
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), _

Signature

Enjoy,
Tony

>> 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
[quoted text clipped - 12 lines]
> Regards,
> Klaus
Klaus Linke - 28 May 2008 19:40 GMT
Oops, thanks! Didn't pay enough attention to the code.

Klaus

>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
[quoted text clipped - 8 lines]
>    KeyCode:=BuildKeyCode(wdKeyF10), _
>    KeyCode2:=BuildKeyCode(wdKeyF1), _
Greg_W - 28 May 2008 21:32 GMT
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?
Tony Jollans - 29 May 2008 00:01 GMT
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), _

Signature

Enjoy,
Tony

> Klaus and Tony,
>
[quoted text clipped - 27 lines]
> customize
> dialog, but cannot do it using VBA?
Greg_W - 29 May 2008 05:04 GMT
Tony,

Your solution works perfectly!

Thanks

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.