[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1332
  • Last Modified:

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.
0
sjgrey
Asked:
sjgrey
  • 3
  • 2
1 Solution
 
Chris BottomleyCommented:
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 BottomleyCommented:
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
 
Chris BottomleyCommented:
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
 
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

Featured Post

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now