Solved

VBA code to assign shortcut key in Word 2007

Posted on 2008-10-26
5
1,403 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
  • 3
  • 2
5 Comments
 
LVL 6

Expert Comment

by:slinkygn
Comment Utility
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
Comment Utility
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
Comment Utility
Sorry, was meant to say I require VBA code for Word 2007
0
 
LVL 6

Expert Comment

by:slinkygn
Comment Utility
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
Comment Utility
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Enums (shorthand for ‘enumerations’) are not often used by programmers but they can be quite valuable when they are.  What are they? An Enum is just a type of variable like a string or an Integer, but in this case one that you create that contains…
This article describes some techniques which will make your VBA or Visual Basic Classic code easier to understand and maintain, whether by you, your replacement, or another Experts-Exchange expert.
This video walks the viewer through the process of creating a watermark for their document, customizing it, and saving it for viewing/printing needs.
This video shows and describes the main difference between both orientations in Microsoft Word. Viewers will understand when to use each orientation and how to get the most out of them.

762 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

8 Experts available now in Live!

Get 1:1 Help Now