L
Lisa
The other day I posted a question re. setting up a user form to assign
shortcut keys to various macros. (Thank you Perry and Jean-Guy for assistance
with this!)
This is sort of an update -- with new code and three new questions... (And
sorry for the excrutiatingly long post.)
Below is the code I finally ended up with. It seems to work the way I want
it to. That is, it assigns a shortcut key to a macro based on user input in a
userform text box. (The shortcut key combo must include Ctrl or Alt or both
and an alphanumeric key -- and may include the Shift key.)
However, I'm new to all of this coding stuff, and would very much appreciate
help fixing a few things if feasible. I've been trying, but I haven't figured
out how.
Specifically:
1) I have a total of 10 text boxes that will be used to assign shortcut keys
to 10 different macros. The KeyDown code for each of these would be identical
except for "TextBox1.text = strKeySequence" at the end of the macro.
(Obviously, that would vary by text box.)
Also the KeyUp code for each could be identical except for the
"strCurrent=TextBox1.text" at the beginning of the macro and the command
assigned to the shortcut key.
I've tried putting the common bits of code in a separate sub, but it won't
compile ("Argument not optional").
How can I push the "meat" of the code out of the field-specific stuff, so I
don't have the same thing repeated 10 times (once for each text box)?
2) I think there's probably a more elegant/streamlined way to do the
keybinding based on what the user enters in the text box. However, my
attempts to use the "strKeySequence" more directly/efficiently have not
worked.
How can I better capture the KeyDown info and use it in the KeyUp code, so I
don't have all those silly-looking "Ifs" in the KeyUp?
3) Everytime I press the Alt key in the (modal) userform text box, I get a
"ding" (error-type sound... I don't know what it's officially called). I
don't get this when pressing other keys (Ctrl, Shift, etc.)
How can I make that annoying dinging stop?
Thanks in advance for any help/guidance.
THE INELEGANT BUT FUNCTIONAL CODE
Public myKey, myChar As String
Private Sub TextBox1_KeyDown(ByVal KeyCode As _
MSForms.ReturnInteger, ByVal Shift As Integer)
'Based on code from Peter Hewett posted to _
microsoft.public.word.vba.general
Const MaskShift As Integer = 1
Const MaskCtrl As Integer = 2
Const MaskAlt As Integer = 4
Dim strKeys As String
' Ignore keystroke sequence unless Ctrl and/or Alt is pressed as well
If Shift And (MaskCtrl Or MaskAlt) Then
' Add Ctrl/Alt/Shift prefix text
If Shift And MaskCtrl Then
strKeys = "Ctrl"
End If
If Shift And MaskAlt Then
If LenB(strKeys) > 0 Then
strKeys = strKeys & "+Alt"
Else
strKeys = "Alt"
End If
End If
If Shift And MaskShift Then
If LenB(strKeys) > 0 Then
strKeys = strKeys & "+Shift"
Else
strKeys = "Shift"
End If
End If
'For use in KeyUp to determine the Ctrl, Alt, and Shift combo for
shortcut key
myKey = strKeys
' Keyboard character
If LenB(strKeys) > 0 Then
strKeys = strKeys & "+" & Chr$(KeyCode)
Else
strKeys = Chr$(KeyCode)
End If
myChar = (KeyCode)
Else: strKeys = ""
End If
TextBox1.Text = strKeys
End Sub
Private Sub TextBox1_KeyUp(ByVal KeyCode As _
MSForms.ReturnInteger, ByVal Shift As Integer)
Dim strCurrent As String
strCurrent = TextBox1.Text
If LenB(strCurrent) = 2 Then
msgbox "need to use ctrl or alt (or both)"
Else
If myKey = "Ctrl" Then
CustomizationContext = ActiveDocument.AttachedTemplate
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, myChar), _
KeyCategory:=wdKeyCategoryMacro, Command:="a_testarea.test1"
End If
If myKey = "Ctrl+Alt" Then
CustomizationContext = ActiveDocument.AttachedTemplate
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, _
wdKeyAlt, myChar), _
KeyCategory:=wdKeyCategoryMacro, Command:="a_testarea.test1"
End If
If myKey = "Ctrl+Shift" Then
CustomizationContext = ActiveDocument.AttachedTemplate
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, _
wdKeyShift, myChar), _
KeyCategory:=wdKeyCategoryMacro, Command:="a_testarea.test1"
End If
If myKey = "Ctrl+Alt+Shift" Then
CustomizationContext = ActiveDocument.AttachedTemplate
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, _
wdKeyAlt, wdKeyShift, myChar), _
KeyCategory:=wdKeyCategoryMacro, Command:="a_testarea.test1"
End If
If myKey = "Alt" Then
CustomizationContext = ActiveDocument.AttachedTemplate
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyAlt, myChar), _
KeyCategory:=wdKeyCategoryMacro, Command:="a_testarea.test1"
End If
If myKey = "Alt+Shift" Then
CustomizationContext = ActiveDocument.AttachedTemplate
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyAlt, _
wdKeyShift, myChar), _
KeyCategory:=wdKeyCategoryMacro, Command:="a_testarea.test1"
End If
End If
myKey = ""
End Sub
shortcut keys to various macros. (Thank you Perry and Jean-Guy for assistance
with this!)
This is sort of an update -- with new code and three new questions... (And
sorry for the excrutiatingly long post.)
Below is the code I finally ended up with. It seems to work the way I want
it to. That is, it assigns a shortcut key to a macro based on user input in a
userform text box. (The shortcut key combo must include Ctrl or Alt or both
and an alphanumeric key -- and may include the Shift key.)
However, I'm new to all of this coding stuff, and would very much appreciate
help fixing a few things if feasible. I've been trying, but I haven't figured
out how.
Specifically:
1) I have a total of 10 text boxes that will be used to assign shortcut keys
to 10 different macros. The KeyDown code for each of these would be identical
except for "TextBox1.text = strKeySequence" at the end of the macro.
(Obviously, that would vary by text box.)
Also the KeyUp code for each could be identical except for the
"strCurrent=TextBox1.text" at the beginning of the macro and the command
assigned to the shortcut key.
I've tried putting the common bits of code in a separate sub, but it won't
compile ("Argument not optional").
How can I push the "meat" of the code out of the field-specific stuff, so I
don't have the same thing repeated 10 times (once for each text box)?
2) I think there's probably a more elegant/streamlined way to do the
keybinding based on what the user enters in the text box. However, my
attempts to use the "strKeySequence" more directly/efficiently have not
worked.
How can I better capture the KeyDown info and use it in the KeyUp code, so I
don't have all those silly-looking "Ifs" in the KeyUp?
3) Everytime I press the Alt key in the (modal) userform text box, I get a
"ding" (error-type sound... I don't know what it's officially called). I
don't get this when pressing other keys (Ctrl, Shift, etc.)
How can I make that annoying dinging stop?
Thanks in advance for any help/guidance.
THE INELEGANT BUT FUNCTIONAL CODE
Public myKey, myChar As String
Private Sub TextBox1_KeyDown(ByVal KeyCode As _
MSForms.ReturnInteger, ByVal Shift As Integer)
'Based on code from Peter Hewett posted to _
microsoft.public.word.vba.general
Const MaskShift As Integer = 1
Const MaskCtrl As Integer = 2
Const MaskAlt As Integer = 4
Dim strKeys As String
' Ignore keystroke sequence unless Ctrl and/or Alt is pressed as well
If Shift And (MaskCtrl Or MaskAlt) Then
' Add Ctrl/Alt/Shift prefix text
If Shift And MaskCtrl Then
strKeys = "Ctrl"
End If
If Shift And MaskAlt Then
If LenB(strKeys) > 0 Then
strKeys = strKeys & "+Alt"
Else
strKeys = "Alt"
End If
End If
If Shift And MaskShift Then
If LenB(strKeys) > 0 Then
strKeys = strKeys & "+Shift"
Else
strKeys = "Shift"
End If
End If
'For use in KeyUp to determine the Ctrl, Alt, and Shift combo for
shortcut key
myKey = strKeys
' Keyboard character
If LenB(strKeys) > 0 Then
strKeys = strKeys & "+" & Chr$(KeyCode)
Else
strKeys = Chr$(KeyCode)
End If
myChar = (KeyCode)
Else: strKeys = ""
End If
TextBox1.Text = strKeys
End Sub
Private Sub TextBox1_KeyUp(ByVal KeyCode As _
MSForms.ReturnInteger, ByVal Shift As Integer)
Dim strCurrent As String
strCurrent = TextBox1.Text
If LenB(strCurrent) = 2 Then
msgbox "need to use ctrl or alt (or both)"
Else
If myKey = "Ctrl" Then
CustomizationContext = ActiveDocument.AttachedTemplate
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, myChar), _
KeyCategory:=wdKeyCategoryMacro, Command:="a_testarea.test1"
End If
If myKey = "Ctrl+Alt" Then
CustomizationContext = ActiveDocument.AttachedTemplate
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, _
wdKeyAlt, myChar), _
KeyCategory:=wdKeyCategoryMacro, Command:="a_testarea.test1"
End If
If myKey = "Ctrl+Shift" Then
CustomizationContext = ActiveDocument.AttachedTemplate
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, _
wdKeyShift, myChar), _
KeyCategory:=wdKeyCategoryMacro, Command:="a_testarea.test1"
End If
If myKey = "Ctrl+Alt+Shift" Then
CustomizationContext = ActiveDocument.AttachedTemplate
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, _
wdKeyAlt, wdKeyShift, myChar), _
KeyCategory:=wdKeyCategoryMacro, Command:="a_testarea.test1"
End If
If myKey = "Alt" Then
CustomizationContext = ActiveDocument.AttachedTemplate
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyAlt, myChar), _
KeyCategory:=wdKeyCategoryMacro, Command:="a_testarea.test1"
End If
If myKey = "Alt+Shift" Then
CustomizationContext = ActiveDocument.AttachedTemplate
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyAlt, _
wdKeyShift, myChar), _
KeyCategory:=wdKeyCategoryMacro, Command:="a_testarea.test1"
End If
End If
myKey = ""
End Sub