Class KeyBinding (Word VBA)

The class KeyBinding represents a custom key assignment in the current context. The KeyBinding object is a member of the KeyBindings collection.

The main procedure of class KeyBinding is KeyBindings.Add

Set

To use a KeyBinding class variable it first needs to be instantiated, for example

Dim kbg as KeyBinding
Set kbg = Dim lngKeyCode As Long: lngKeyCode = 
FindKey(KeyCode:=lngKeyCode)

The following procedures can be used to set variables of type KeyBinding: Application.FindKey, KeyBindings.Add, KeyBindings.Item, KeyBindings.Key, KeysBoundTo.Item, KeysBoundTo.Key, Application.KeyBindings and Application.KeysBoundTo

For Each

Here is an example of processing the KeyBinding items in a collection.

Dim lngCode As Long 
Dim kbg As KeyBinding 
CustomizationContext = NormalTemplate 
lngCode = BuildKeyCode(wdKeyAlt, wdKeyControl, wdKeyW) 
For Each kbg In KeyBindings 
 If lngCode = kbg.KeyCode Then MsgBox kbg.KeyString & " is already in use" 
Next kbg

Methods

This is the main method of the KeyBinding class

KeyBindings.Add - Returns a KeyBinding object that represents a new shortcut key for a macro, built-in command, font, AutoText entry, style, or symbol.

Dim strCommand As String: strCommand = 
Dim lngKeyCode As Long: lngKeyCode =  
Dim kbg As KeyBinding
Set kbg = KeyBindings.Add(KeyCategory:=wdKeyCategoryAutoText, Command:=strCommand, KeyCode:=lngKeyCode)

Other Methods

Rebind - Changes the command assigned to the specified key binding.

Dim strCommand As String: strCommand =  
FindKey.Rebind KeyCategory:=wdKeyCategoryAutoText, Command:=strCommand

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

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

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

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

Properties

Command returns the command assigned to the specified key combination.

Dim strCommand As String
strCommand = FindKey.Command

CommandParameter returns the command parameter assigned to the specified shortcut key.

Dim strCommandParameter As String
strCommandParameter = FindKey.CommandParameter

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

Dim docContext As Document
Set docContext = FindKey.Context

KeyCategory returns the type of item assigned to the specified key binding.

Dim wkcKeyCategory As WdKeyCategory
wkcKeyCategory = FindKey.KeyCategory

KeyCode returns a unique number for the first key in the specified key binding.

Dim lngKeyCode As Long
lngKeyCode = FindKey.KeyCode

KeyCode2 returns a unique number for the second key in the specified key binding.

Dim lngKeyCode2 As Long
lngKeyCode2 = FindKey.KeyCode2

KeyString returns the key combination string for the specified keys (for example, CTRL+SHIFT+A).

Dim strKeyString As String
strKeyString = FindKey.KeyString

Parent returns an object that represents the parent object of the specified KeyBinding object.

Dim objParent As Object
Set objParent = FindKey.Parent

Protected true if you cannot change the specified key binding in the Customize Keyboard dialog box.

Dim booProtected As Boolean
booProtected = FindKey.Protected

KeyBindings.Context returns an Object that represents the storage location of the specified key binding.

Dim appContext As Application
Set appContext = KeyBindings.Context

KeyBindings.Count returns a Long that represents the number of key bindings in the collection.

Dim lngCount As Long
lngCount = KeyBindings.Count

KeyBindings.Parent returns an object that represents the parent object of the specified KeyBindings object.

Dim objParent As Object
Set objParent = KeyBindings.Parent

KeysBoundTo.Command returns a String that represents the command assigned to the specified key combination.

Dim strCommand As String
strCommand = KeysBoundTo.Command

KeysBoundTo.CommandParameter returns the command parameter assigned to the specified shortcut key.

Dim strCommandParameter As String
strCommandParameter = KeysBoundTo.CommandParameter

KeysBoundTo.Context returns an Object that represents the storage location of the specified key binding.

Dim docContext As Document
Set docContext = KeysBoundTo.Context

KeysBoundTo.Count returns a Long that represents the number of keys in the collection.

Dim lngCount As Long
lngCount = KeysBoundTo.Count

KeysBoundTo.KeyCategory returns the type of item assigned to the specified key binding.

Dim wkcKeyCategory As WdKeyCategory
wkcKeyCategory = KeysBoundTo.KeyCategory

KeysBoundTo.Parent returns an object that represents the parent object of the specified KeysBoundTo object.

Dim objParent As Object
Set objParent = KeysBoundTo.Parent