Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Assign Keyboard shortcut to Word Sub via code

Posted on 2007-11-16
7
Medium Priority
?
247 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 560 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 

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

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

Question has a verified solution.

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

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
Introduction Authors who set out to write any sort of lengthy piece for online submission—be it a long question or comment on a technical form, an article, or a substantial blog entry—often find it useful to work up a draft in an editor other t…
This video walks the viewer through the process of creating envelopes and labels, with multiple names and addresses. Navigate to the “Start Mail Merge” button in the Mailings tab: Follow the step-by-step process until asked to find the address doc…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.

721 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