I would like to add KeyBindings that will call a sub with a parameter. E.g.:
Sub dummy(text As String)
MsgBox (text)
End Sub
Dim kbNew As KeyBinding
Set kbNew = KeyBindings.Add(KeyCategory:=wdKeyCategoryCommand, Command:="dummy", KeyCode:=BuildKeyCode(wdKeyControl, wdKeyAlt, wdKeyQ), CommandParameter:="show me")
This is not working. But when I use example from MS docs, it's working:
Set kbNew = KeyBindings.Add(KeyCategory:=wdKeyCategoryCommand, Command:="FontSize", KeyCode:=BuildKeyCode(wdKeyControl, wdKeyAlt, wdKeyQ), CommandParameter:="8")
Calling a sub without parameter is working also:
Sub dummy()
MsgBox ("show me")
End Sub
Set kbNew = KeyBindings.Add(KeyCategory:=wdKeyCategoryCommand, Command:="dummy", KeyCode:=BuildKeyCode(wdKeyControl, wdKeyAlt, wdKeyQ))
Only the built-in commands support passing a Command Parameter. For "ordinary" macros it's not possible.
When the problem is looked at logically, pressing a key combination that applies the font size 8 is static - it will always apply font size 8 and not anything else. Pressing a key combination to run a custom procedure is therefore also inherently static - how should the key combination be able to deal with a variable input? There's no facility for passing anything dynamically.
If the procedure called by the key combination needs to work with variable information, then this would be need to be stored somewhere and the macro can read it. This could be, for example, text in bookmark or content control in the document, the value of a Document Variable
or a node in a CustomXMLPart
, or content stored externally in a file or the Registry. Or have the macro display an InputBox
.