Link to home
Start Free TrialLog in
Avatar of CISmacro
CISmacro

asked on

VBA code to assign shortcut key in Word 2007

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
Avatar of slinkygn
slinkygn
Flag of United States of America image

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
Avatar of CISmacro
CISmacro

ASKER

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.
Sorry, was meant to say I require VBA code for Word 2007
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.
ASKER CERTIFIED SOLUTION
Avatar of CISmacro
CISmacro

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial