Solved

Assign Keyboard shortcut to Word Sub via code

Posted on 2007-11-16
7
241 Views
Last Modified: 2012-06-21
I have a sub below that I want the users to be able to execute in the document by a Keyboard Shortcut Alt Q.  I know how to assign this using Tools Customize Keyboard, but is there a way to assign that shortcut in the Sub itself via code?  Thanks


Sub Insert_Data()
Selection.TypeText Text:="THIS IS TEXT TO INSERT RIGHT HERE"
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Font.Color = wdColorBlack
Selection.Font.Bold = True
Selection.HomeKey Unit:=wdLine

End Sub
0
Comment
Question by:quizzer
  • 4
  • 3
7 Comments
 
LVL 59

Accepted Solution

by:
Chris Bottomley earned 140 total points
ID: 20301232
Sub Bind_Shortcuts()
Dim bound As KeyBinding
    Set bound = Application.FindKey(BuildKeyCode(wdKeyQ, wdKeyAlt))
    If bound.Command = "" Then
        KeyBindings.Add wdKeyCategoryMacro, "Insert_Data", BuildKeyCode(wdKeyQ, wdKeyAlt)
    Else
        bound.Rebind wdKeyCategoryMacro, "Insert_Data", BuildKeyCode(wdKeyQ, wdKeyAlt)
    End If
   
Set bound = Nothing
End Sub

Regards
Chris
0
 

Author Comment

by:quizzer
ID: 20301438
Chris,
Thanks for responding.  So if for example I wanted to change the keyboard shortcut from Alt Q to Alt K all I would have to do is change the Q to K like below in the code, without needing to go in and assign the keysthrough Tools Customize Keyboard/

Sub Bind_Shortcuts()
Dim bound As KeyBinding
    Set bound = Application.FindKey(BuildKeyCode(wdKeyK, wdKeyAlt))
    If bound.Command = "" Then
        KeyBindings.Add wdKeyCategoryMacro, "Insert_Data", BuildKeyCode(wdKeyK, wdKeyAlt)
    Else
        bound.Rebind wdKeyCategoryMacro, "Insert_Data", BuildKeyCode(wdKeyK, wdKeyAlt)
    End If
   
Set bound = Nothing
End Sub
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 20301480
Yup and similarly for wdkeyshift, wdkeycontrol combinations i.e:

BuildKeyCode(wdKeyK, wdKeyAlt, wdKeyControl)

would mean the ctrl alt K key combination

Chris
0
On Demand Webinar - Networking for the Cloud Era

This webinar discusses:
-Common barriers companies experience when moving to the cloud
-How SD-WAN changes the way we look at networks
-Best practices customers should employ moving forward with cloud migration
-What happens behind the scenes of SteelConnect’s one-click button

 

Author Comment

by:quizzer
ID: 20301587
Tried This Using Alt K and it doesn't work.  Any ideas???

Sub Bind_Shortcuts()
Dim bound As KeyBinding
    Set bound = Application.FindKey(BuildKeyCode(wdKeyAlt, wdKeyK))
    If bound.Command = "" Then
        KeyBindings.Add wdKeyCategoryMacro, "Insert_Data", BuildKeyCode(wdKeyAlt, wdKeyK)
    Else
        bound.Rebind wdKeyCategoryMacro, "Insert_Data", BuildKeyCode(wdKeyAlt, wdKeyK)
    End If
   
Set bound = Nothing
End Sub
Sub Insert_Data()
'
' Insert_Data Macro
' Macro recorded 11/14/2007 by David Vessey



Selection.TypeText Text:="THIS IS TEXT TO INSERT RIGHT HERE"
Selection.HomeKey Unit:=wdLine
Selection.EndKey Unit:=wdLine, Extend:=wdExtend
Selection.Font.Color = wdColorBlack
Selection.Font.Bold = True
Selection.HomeKey Unit:=wdLine

End Sub
0
 

Author Comment

by:quizzer
ID: 20301677
I figured it out.  You have to run the Sub
Bind_Shortcuts

when the Document Opens in order for the shortcut you designate to be available.
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 20301679
Can you give any clues ... i've double checked and all works fine in that that the binding works fine.

Chris
0
 
LVL 59

Expert Comment

by:Chris Bottomley
ID: 20301688
Sorry ... didn't realise the general situation.  Yes you are absolutely right it needs to be run on open for any using document.

Chris
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Like many others, we try and discourage users from printing documents unnecessarily and instead send or share them electronically. However, this doesn't always work and documents are still printed. With this simple solution, if the user tries to …
This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
In this video, we show how to convert an image-only PDF file into a PDF Searchable Image file, that is, a file with both the image (typically from scanning) and text, which is created in an automated fashion with Optical Character Recognition (OCR) …
This video walks the viewer through the process of creating Hyperlinks for the web and other documents. Select the "Insert" tab: Click "Hyperlink":  Type "http://" followed by a web address to reference a website or navigate to a document to ref…

685 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question