none
Unable to compare Command property during ittering over KeyBindings RRS feed

  • Question

  • A KeyBinding can be added with VBA to the KeyBindings collection using the KeyBindings object. Like this (or you can do the same manunaly (in the Customize Keyboarddialog box):

    KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyInsert), KeyCategory:=wdKeyCategoryMacro, Command:="MacroName"

    As you see a macro has to be add to the categorie wdKeyCategoryMacro.

    A build-in command, like the command for the navigation pane, has to be add to wdKeyCategoryCommand:

    KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyControl, wdKeyN), KeyCategory:=wdKeyCategoryCommand, Command:="NavPane"

    Both of these KeyBindings now have a property Command: the first "MacroName", the second "NavPane".

    What I want to do is to retrieve the value of the Command property and than take some action. To retrive the Command property of a build-in KeyBinding of the categorie wdKeyCategoryCommand is no problem. In this case "NavPane" is printed. But if the Command property of the category wdKeyCategoryMacro is tested, no value is printed. In other words: that command seems to have no value but that's not possible.

        Dim kb      As KeyBinding
        
        For Each kb In KeyBindings
            If kb.Command = "MacroName" Then
                Debug.Print kb.KeyString & vbTab & kb.Command
                Exit For
            End If
        Next kb

    Can someone tell me why the value of the Command property (especially of the wdKeyCategoryMacro category) cannot be retrieved?

    Thanks

    Saturday, May 4, 2019 8:22 PM

Answers

  • It's probably because when you set up a KeyBinding to a macro, Word stores a fully qualified reference to the Sub, e.g. instead of "MacroName" it might store "Normal.ModuleName.MacroName" where "Normal" is the template/document project and "ModuleName" is the name of the module that contains the macro.

    You could test that by changing your If kb.COmmand line to 

    If kbCommand Like "*MacroName*" Then



    Peter Jamieson

    Sunday, May 5, 2019 2:10 PM
  • In the meantime I have found a better solution. Note the category that is currently being searched. Although I want the KeyString that is assigned to a macro, the Command category must now be searched. That's what I did wrong.

    It is also important to take into account that multiple KeyBindings can be assigned to one macro. Only the macro name (or the name of another Command) needs to be passed with the strCommand argument.

    Function GetKeyString(strCommand As String) As String
    
        Dim kb As KeyBinding
        Dim strBuffer As String
        
        CustomizationContext = NormalTemplate
        
        For Each kb In KeysBoundTo(KeyCategory:=wdKeyCategoryCommand, Command:=strCommand)
            strBuffer = strBuffer & kb.KeyString & vbNewLine
        Next kb
        
        GetKeyString = strBuffer
    End Function

    Sunday, May 5, 2019 8:00 PM

All replies

  • It's probably because when you set up a KeyBinding to a macro, Word stores a fully qualified reference to the Sub, e.g. instead of "MacroName" it might store "Normal.ModuleName.MacroName" where "Normal" is the template/document project and "ModuleName" is the name of the module that contains the macro.

    You could test that by changing your If kb.COmmand line to 

    If kbCommand Like "*MacroName*" Then



    Peter Jamieson

    Sunday, May 5, 2019 2:10 PM
  • Thanks Peter, it works. Stupid of me that I have not thought of that before.
    Sunday, May 5, 2019 6:44 PM
  • In the meantime I have found a better solution. Note the category that is currently being searched. Although I want the KeyString that is assigned to a macro, the Command category must now be searched. That's what I did wrong.

    It is also important to take into account that multiple KeyBindings can be assigned to one macro. Only the macro name (or the name of another Command) needs to be passed with the strCommand argument.

    Function GetKeyString(strCommand As String) As String
    
        Dim kb As KeyBinding
        Dim strBuffer As String
        
        CustomizationContext = NormalTemplate
        
        For Each kb In KeysBoundTo(KeyCategory:=wdKeyCategoryCommand, Command:=strCommand)
            strBuffer = strBuffer & kb.KeyString & vbNewLine
        Next kb
        
        GetKeyString = strBuffer
    End Function

    Sunday, May 5, 2019 8:00 PM