Class KeyBindings (Word VBA)

A collection of KeyBinding objects that represent the custom key assignments in the current context. Custom key assignments are made in the Customize Keyboard dialog box. To use a KeyBindings class variable it first needs to be instantiated, for example


Dim kbs as KeyBindings
Set kbs = KeyBindings

For Each

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


Dim kbg As KeyBinding
For Each kbg In KeyBindings
	
Next kbg

Add

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

You can use the BuildKeyCode method to create the KeyCode or KeyCode2 argument. In the following table, the left column contains commands that require a command value, and the right column describes what you must do to specify CommandParameter for each of these commands. (The equivalent action in the Customize Keyboard dialog box (Tools menu) to specifying CommandParameter is selecting an item in the list box that appears when you select one of the following commands in the Commands box.)

Add (KeyCategory, Command, KeyCode, KeyCode2, CommandParameter)


CustomizationContext = NormalTemplate 
KeyBindings.Add _ 
    KeyCategory:=wdKeyCategoryCommand, _ 
    Command:="FileClose", _ 
    KeyCode:=BuildKeyCode(wdKeyControl, wdKeyAlt, wdKeyW)

Arguments

The following arguments are required:

KeyCategory (WdKeyCategory) - The category of the key assignment.

Here you can find possible values for WdKeyCategory

Command (String) - The command that the specified key combination executes.

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

Optional arguments

The following arguments are optional

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

Here you can find possible values for WdKey

CommandParameter (String) - Additional text, if any, required for the command specified by Command. For details, see the Remarks section below.

ClearAll

Clears all the customized key assignments and restores the original Microsoft Word shortcut key assignments.


CustomizationContext = NormalTemplateKeyBindings.ClearAll

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.


Dim appContext As Application
Set appContext = KeyBindings.Context

Count

Returns a Long that represents the number of key bindings in the collection.


Dim lngCount As Long
lngCount = KeyBindings.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 kbg As KeyBinding
Set kbg = KeyBindings(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)


CustomizationContext = NormalTemplate 
KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyAlt, wdKeyF4), _ 
 KeyCategory:=wdKeyCategoryFont, Command:="Arial" 
MsgBox KeyBindings.Count & " keys in KeyBindings collection" 
KeyBindings.Key(KeyCode:=BuildKeyCode(wdKeyAlt, wdKeyF4)).Clear 
MsgBox KeyBindings.Count & " keys in KeyBindings collection"

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