Solved

Assign Keyboard shortcut to Word Sub via code

Posted on 2007-11-16
7
244 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

 

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

Enroll in July's Course of the Month

July's Course of the Month is now available! Enroll to learn HTML5 and prepare for certification. It's free for Premium Members, Team Accounts, and Qualified Experts.

Question has a verified solution.

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

It is often necessary in this forum and others to illustrate Word fields as text with the field delimiters replaced with the curly brackets that the delimiters resemble when field codes are being displayed on the document. This means that the text c…
I'm writing to share my clumsy experience in using this elegant tool so you can avoid every stupid mistake I made. (I leave it to the authorities to decide if this deserves a place in the Knowledge archives.)  Now that I am on the other side of my l…
This video shows the viewer how to set up and create Footnotes in their document. Click on the References tab: Select "Insert Footnote": Type in desired text:
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …

632 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