Solved

Assign Keyboard shortcut to Word Sub via code

Posted on 2007-11-16
7
237 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
Preface: When I started this series, I used the term CommandBars because that is the Office Object class that it discusses. Unfortunately, when Microsoft introduced Office 2007, they replaced the standard Commandbar menus with "The Ribbon" and rem…
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 Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

914 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now