Context Property

Microsoft Word Visual Basic

Context Property

       

Returns an object that represents the storage location of the specified key binding. This property can return a Document, Template, or Application object. Read-only.

Note   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.

Example

This example displays the name of the document or template where the macro named "Macro1" is stored.

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

This example adds the F2 key to the Italic command and then uses the For Each...Next loop to display the keys assigned to the Italic command along with the context.

Dim kbLoop As KeyBinding

CustomizationContext = NormalTemplate
KeyBindings.Add KeyCategory:=wdKeyCategoryCommand, _
    Command:="Italic", KeyCode:=wdKeyF2
For Each kbLoop In _
        KeysBoundTo(KeyCategory:=wdKeyCategoryCommand, _
        Command:="Italic")
    MsgBox kbLoop.KeyString & vbCr & kbLoop.Context.Name
Next kbLoop