Class KeysBoundTo (Word VBA)

A collection of KeyBinding objects assigned to a command, style, macro, or other item in the current context. To use a KeysBoundTo class variable it first needs to be instantiated, for example


Dim kbt as KeysBoundTo
Set kbt = Dim strCommand As String: strCommand = 
KeysBoundTo(KeyCategory:=wdKeyCategoryAutoText, Command:=strCommand)

Command

Returns a String that represents the command assigned to the specified key combination.


Dim kbLoop As KeyBinding 
 
For Each kbLoop In KeyBindings 
 If kbLoop.KeyCategory = wdKeyCategoryFont Then 
 Count = Count + 1 
 MsgBox kbLoop.Command & vbCr & kbLoop.KeyString 
 End If 
Next kbLoop 
 
If Count = 0 Then MsgBox "Keys haven't been assigned to fonts"

CommandParameter

Returns the command parameter assigned to the specified shortcut key.

For information about commands that take parameters, see the Add method. Use the Command property to return the command name assigned to the specified shortcut key.


Dim kbNew As KeyBinding 
 
Set kbNew = KeyBindings.Add(KeyCategory:=wdKeyCategoryCommand, _ 
 Command:="FontSize", _ 
 KeyCode:=BuildKeyCode(wdKeyControl, wdKeyAlt, wdKeyS), _ 
 CommandParameter:="8") 
MsgBox kbNew.Command & Chr$(32) & kbNew.CommandParameter _ 
 & vbCr & kbNew.KeyString

Context

Returns an Object that represents the storage location of the specified key binding.

This property can return a Document, Template, or Application object. Built-in key assignments (for example, CTRL+I for Italic) return the Application object as the context. Any key bindings you add will return a Document or Template object, depending on the customization context in effect when the KeyBinding object was added.


Sub TestContext1() 
 Dim kbMacro1 As KeysBoundTo 
 
 Set kbMacro1 = KeysBoundTo(KeyCategory:=wdKeyCategoryMacro, _ 
 Command:="Macro1") 
 MsgBox kbMacro1.Context.Name 
End Sub

Count

Returns a Long that represents the number of keys in the collection.


Dim lngCount As Long
lngCount = KeysBoundTo.Count

Item

Returns an individual KeyBinding object in a collection.

Item (Index)

Index: The individual object to be returned. Can be a Long indicating the ordinal position of the individual object.


Dim kbgKeysBoundTo As KeyBinding
Set kbgKeysBoundTo = KeysBoundTo(Index:=1)

Key

Returns a KeyBinding object that represents the specified custom key combination.

If the key combination doesn't exist, this method returns Nothing. You can use the BuildKeyCode method to create the KeyCode or KeyCode2 argument.

Key (KeyCode, KeyCode2)


Dim lngKeyCode As Long: lngKeyCode =  
Dim kbgKey As KeyBinding
Set kbgKey = KeysBoundTo.Key(KeyCode:=lngKeyCode)

Arguments

The following argument is required

KeyCode (Long) - A key you specify by using one of the WdKey constants.

Optional arguments

The following argument is optional

KeyCode2 (WdKey) - A second key you specify by using one of the WdKey constants.

Here you can find possible values for WdKey

KeyCategory

Returns the type of item assigned to the specified key binding. Here you can find possible values for WdKeyCategory.


Dim wkcKeyCategory As WdKeyCategory
wkcKeyCategory = KeysBoundTo.KeyCategory