Assign subroutine HandleShortcutKey to any shortcut key you want to handle."
Did you actually try this? I did. I copied your code, and assigned Alt-R to it.
What happens? You
need to press Alt-R to execute HandleShortcutKey...as THAT is the assigned shortcut. Right?
So...
nothing happens, as in your code, there is no "r". Sure, IF you put an R in, like:
Code:
Public Sub HandleShortcutKey()
If (GetKeyState(vbKeyA) < 0) Then
' Handle Alt-A
ElseIf (GetKeyState(vbKeyM) < 0) Then
' Handle Alt-M
ElseIf (GetKeyState(vbKeyZ) < 0) Then
' Handle Alt-Z
ElseIf (GetKeyState(vbKeyR) < 0) Then
Msgbox "R is pressed."
End If
End Sub
You will get "R is pressed." as THAT is the key pressed...but no other is possible. Even if you make ANOTHER procedure:
Code:
Sub ThisIs_AltA()
MsgBox "A"
End Sub
and assign THAT Alt-A. Test it. Pressing Alt-A will display:
"A"
OK? So...Alt-A
does work....by itself.
Now, change the code a little, so that a "A" should - according to you - "handle" Alt-A. Remember, Alt-A,
by itself DOES work. But, also remember you stated: "Assign subroutine HandleShortcutKey to any shortcut key you want to handle." I assigned Alt-R to HandleShortcutKey.
Code:
Public Sub HandleShortcutKey()
If (GetKeyState(vbKeyA) < 0) Then
' Handle Alt-A
Call ThisIs_AltA
ElseIf (GetKeyState(vbKeyM) < 0) Then
' Handle Alt-M
MsgBox "M?"
ElseIf (GetKeyState(vbKeyZ) < 0) Then
' Handle Alt-Z
MsgBox "Z?"
ElseIf (GetKeyState(vbKeyR) < 0) Then
' handle Alt-R
MsgBox "R?"
End If
End Sub
What happens?
Msgbox "R" is what happens. Again...HOW is it going to get an "A". NO "A" was pressed. The only key pressed was the "R", needed to execute HandleShortcutKey in the first place.
Again, HOW are you getting a parameter? You simply can not. There IS no parameter. You can NOT get an "A", or a "M", or a "Z"...because you pressed "R" (assuming that is what you assigned as a shortcut).
It does NOT matter what shortcut key you assigned, the only value you can get is THAT key. Put another way:
Code:
' shortcut key = Alt-R
Public Sub HandleShortcutKey()
If (GetKeyState(vbKeyA) < 0) Then
' Handle Alt-A
Selection.EndKey Unit:=wdStory
Alt-R executes Sub HandleShortcutKey.
Sub HandleShortcutKey executes, but it will NEVER execute (or "handle") the instructions for Alt-A...even if there IS a procedure with an Alt-A shortcut.
I
have a procedure with an Alt-A shortcut. But as I press Alt-R to execute Sub HandleShortcutKey, where is it getting the "A"? Even if you directly Call the procedure, it will not execute. Even if you just have the instructions, it will not execute.
The only value that can be returned is the key you pressed for the shortcut you assign to HandleShortcutKey. You can put as many ElseIf as you want (the whole alphabet if you want), the only one that will do anything is...the key you pressed as the shortcut key to execute HandleShortcutKey.
Unless I am missing something drastic I simply can not see how this will work.
faq219-2884
Gerry
My paintings and sculpture