Add Method (KeyBindings Collection)
Applies To
KeyBindings collection object.
Description
Adds a new KeyBinding object to the KeyBindings collection. A KeyBinding object represents a custom key assignment, which includes a key code, a key category, and a command.
Syntax
expression.Add(KeyCategory, Command, KeyCode, KeyCode2, CommandParameter)
expression Required. An expression that returns a KeyBindings object.
KeyCategory Required Long. The category of the key assignment. Can be one of the following WdKeyCategory constants:
Command Required String. The command that the specified key combination executes.
KeyCode Required Long. A key you specify by using one of the WdKey constants.
KeyCode2 Optional Variant. A second key you specify by using one of the WdKey constants.
CommandParameter Optional Variant. Additional text, if any, required for the command specified by Command. For details, see the "Remarks" section.
Remarks
You can use the BuildKeyCode method to create the KeyCode or KeyCode2 argument.
In the following table, the left-hand column contains commands that require a command value, and the right-hand 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.)
If Command is set to | CommandParameter must be |
|
Borders, Color, or Shading | A number — specified as text — corresponding to the position of the setting selected in the list box that contains values, where 0 (zero) is the first item, 1 is the second item, and so on. |
Columns | A number between 1 and 45 — specified as text — corresponding to the number of columns you want to apply. |
Condensed | A text measurement between 0.1 point and 12.75 points specified in 0.05-point increments (72 points = 1 inch). |
Expanded | A text measurement between 0.1 point and 12.75 points specified in 0.05-point increments (72 points = 1 inch). |
FileOpenFile | The path and file name of the file to be opened. If the path isn't specified, the current folder is used. |
Font Size | A positive text measurement, specified in 0.5-point increments (72 points = 1 inch). |
Lowered, Raised | A text measurement between 1 point and 64 points, specified in 0.5-point increments (72 points = 1 inch). |
Symbol | A string created by concatenating a Chr( ) instruction and the name of a symbol font (for example, Chr(167) & "Symbol"). |
See Also
Clear method, CustomizationContext property, Disable method.
Example
This example adds the CTRL+ALT+W key combination to the FileClose command. The keyboard customization is saved in the Normal template.
CustomizationContext = NormalTemplate
KeyBindings.Add KeyCategory:=wdKeyCategoryCommand, Command:="FileClose", KeyCode:=BuildKeyCode(wdKeyControl, wdKeyAlt, wdKeyW)
This example adds the ALT+F4 key combination to the Arial font and then displays the number of items in the KeyBindings collection. The example then clears the ALT+F4 key combination (returned it to its default setting) and redisplays the number of items in the KeyBindings collection.
CustomizationContext = ActiveDocument.AttachedTemplate
Set myKey = KeyBindings.Add(KeyCategory:=wdKeyCategoryFont, _
Command:="Arial", KeyCode:=BuildKeyCode(wdKeyAlt, wdKeyF4))
MsgBox KeyBindings.Count & " keys in KeyBindings collection"
myKey.Clear
MsgBox KeyBindings.Count & " keys in KeyBindings collection"
This example adds the CTRL+ALT+S key combination to the Font command with 8 points specified for the font size.
CustomizationContext = NormalTemplate
KeyBindings.Add KeyCategory:=wdKeyCategoryCommand, Command:="FontSize", _
KeyCode:=BuildKeyCode(wdKeyControl, wdKeyAlt, wdKeyS), _
CommandParameter:="8"
This example adds the CTRL+ALT+H key combination to the Heading 1 style in the active document.
CustomizationContext = ActiveDocument
KeyBindings.Add KeyCategory:=wdKeyCategoryStyle, Command:="Heading 1", _
KeyCode:=BuildKeyCode(wdKeyControl, wdKeyAlt, wdKeyH)
This example adds the CTRL+ALT+O key combination to the AutoText entry named "Hello."
CustomizationContext = ActiveDocument
KeyBindings.Add KeyCategory:=wdKeyCategoryAutoText, Command:="Hello", _
KeyCode:=BuildKeyCode(wdKeyControl, wdKeyAlt, wdKeyO)