Assign macro to keystroke using VBA in Word 2010

I wish to use the Document_open() event to assign a macro to a key, Ctrl-q, when a Word 2010 file opens.

All I've found by searching here and elsewhere on the web is references to earlier versions of Word.
LVL 1
sjgreyAsked:
Who is Participating?
 
Chris BottomleyConnect With a Mentor Software Quality Lead EngineerCommented:
The scope is whatever you want so assuming you retain ctrl_q then the following in thisdocument restricts it to the actual document:

Private Sub Document_Close()

Dim bound As KeyBinding

    CustomizationContext = ActiveDocument
    Set bound = ActiveDocument.Application.FindKey(BuildKeyCode(wdKeyQ, wdKeyControl))
    bound.Disable

End Sub

Private Sub Document_Open()
    CustomizationContext = ActiveDocument
    Set bound = ActiveDocument.Application.FindKey(BuildKeyCode(wdKeyQ, wdKeyControl))
    If bound.Command = "" Then
        KeyBindings.Add wdKeyCategoryMacro, "ctrl_Q", BuildKeyCode(wdKeyQ, wdKeyControl)
    Else
        bound.Rebind wdKeyCategoryMacro, "ctrl_Q", BuildKeyCode(wdKeyQ, wdKeyControl)
    End If


End Sub

Open in new window

0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
Ought not to be any different for example just tested the following:

Sub Bind_Shortcuts()
Dim bound As KeyBinding

    CustomizationContext = ActiveDocument.AttachedTemplate 'NormalTemplate
    Set bound = ActiveDocument.Application.FindKey(BuildKeyCode(wdKeyT, wdKeyAlt))
    If bound.Command = "" Then
        KeyBindings.Add wdKeyCategoryMacro, "crbMacro1", BuildKeyCode(wdKeyT, wdKeyAlt)
    Else
        bound.Rebind wdKeyCategoryMacro, "crbMacro1", BuildKeyCode(wdKeyT, wdKeyAlt)
    End If

End Sub

Public Sub crbMacro1()
    MsgBox "hI I'M nUMBER 1!"
End Sub

Sub unBind_Shortcuts()
Dim bound As KeyBinding

    CustomizationContext = ActiveDocument.AttachedTemplate  '.NormalTemplate
    Set bound = ActiveDocument.Application.FindKey(BuildKeyCode(wdKeyT, wdKeyAlt))
    bound.Disable

End Sub

Open in new window


Chris
0
 
sjgreyAuthor Commented:
Thanks

I had stumbled onto the OnKey technique so might have been looking in the wrong place

Is the reference to the template essential?

Steve
0
 
Chris BottomleySoftware Quality Lead EngineerCommented:
And for what it's worth just tested:

'ThisDocument
Private Sub orig_Document_Close()
Dim bound As KeyBinding

    CustomizationContext = ActiveDocument '.NormalTemplate
    Set bound = ActiveDocument.Application.FindKey(BuildKeyCode(wdKeyQ, wdKeyControl))
    bound.Disable

End Sub

Private Sub orig_Document_Open()
    
    CustomizationContext = ActiveDocument.AttachedTemplate 'NormalTemplate
    Set bound = ActiveDocument.Application.FindKey(BuildKeyCode(wdKeyQ, wdKeyControl))
    If bound.Command = "" Then
        KeyBindings.Add wdKeyCategoryMacro, "ctrl_Q", BuildKeyCode(wdKeyQ, wdKeyControl)
    Else
        bound.Rebind wdKeyCategoryMacro, "ctrl_Q", BuildKeyCode(wdKeyQ, wdKeyControl)
    End If

End Sub

Open in new window


'Module1
Public Sub ctrl_Q()
    MsgBox "Hi i'm control-Q!"
End Sub

Open in new window

0
 
sjgreyAuthor Commented:
Just what I needed thanks

For anyone following this later, in case it helps, here is the code I've implemented and it seems to work, so far ...

Private Sub Document_open()

    Dim bound As KeyBinding

    CustomizationContext = ActiveDocument
    Set bound = ActiveDocument.Application.FindKey(BuildKeyCode(wdKeyQ, wdKeyControl))
    If bound.Command = "" Then
        KeyBindings.Add wdKeyCategoryMacro, "Analyse", BuildKeyCode(wdKeyQ, wdKeyControl)
    Else
        bound.Rebind wdKeyCategoryMacro, "Analyse", BuildKeyCode(wdKeyQ, wdKeyControl)
    End If

End Sub

Private Sub Document_close()

    Dim bound As KeyBinding

    CustomizationContext = ActiveDocument
    Set bound = ActiveDocument.Application.FindKey(BuildKeyCode(wdKeyQ, wdKeyControl))
    bound.Disable

End Sub
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.