Solved

VBA code to assign shortcut key in Word 2007

Posted on 2008-10-26
5
1,430 Views
Last Modified: 2013-11-25
Hello, any help will be greatly appreciated.

I have a basic macro (See below)
I can manually assign a shortcut key to the macro using the mouse.
Is it possible to assign the shortcut key with VBA code for Word 2007?

Macro is auto-loaded from Add-in.

Sub COEShowText()
'
' COEShowText Macro
' COE Show Trainer Text
'
    Selection.Find.ClearFormatting
    Selection.Find.Font.Color = -603914241
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Font.Color = 15073280
    With Selection.Find
        .Text = ""
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub
0
Comment
Question by:CISmacro
[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
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:slinkygn
ID: 22810393
Think you want Application.OnKey.

Application.OnKey "^b", "ToWord"

sets to Ctrl-b.  (^ = ctrl; % = alt; + = shift)

For more info:
http://vbadud.blogspot.com/2007/06/assigning-shortcut-keys-excel-macros.html
0
 

Author Comment

by:CISmacro
ID: 22817503
Thanks for the quick reply but the link refers to a macro for Shortcut keys to Excel 2007.
Assigning Shortcut Keys - Excel Macros
I require macro code for Excel.
0
 

Author Comment

by:CISmacro
ID: 22817531
Sorry, was meant to say I require VBA code for Word 2007
0
 
LVL 6

Expert Comment

by:slinkygn
ID: 22818010
No problem.  Please reread the link.

Application.OnKey is VBA code.  The article is for a macro, but Application.OnKey can take a subroutine name just as easily.

You can also, if you like, use the KeyPress event of the form.  The callback will give you a key code and a shift state; start off with an If for the key combination you want and have it call your function.

But attaching that to the form can run into problems when the form doesn't have focus.  I would opt for the first way.
0
 

Accepted Solution

by:
CISmacro earned 0 total points
ID: 22818430
Thanks for your help.

I end up basing the macro on the below VBA code.

Sub KBShrtcutAssign()
'
' KBShrtcutAssign Macro
'
'
    CustomizationContext = NormalTemplate
    KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyH, wdKeyAlt), KeyCategory:= _
        wdKeyCategoryCommand, Command:="COEHideText"
    CustomizationContext = NormalTemplate
    KeyBindings.Add KeyCode:=BuildKeyCode(wdKeyS, wdKeyAlt), KeyCategory:= _
        wdKeyCategoryCommand, Command:="COEShowText"
End Sub
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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…
Using Word 2013, I was experiencing some incredible lag when typing.  Here's what worked for me....
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…
Learn how to create and modify your own paragraph styles in Microsoft Word. This can be helpful when wanting to make consistently referenced styles throughout a document or template.

738 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