Solved

VBA code to assign shortcut key in Word 2007

Posted on 2008-10-26
5
1,409 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
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

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

Microsoft Word is a program we have all encountered at some point, but very few of us have dug deep into its full scope of features, let alone customized it to suit our needs. Luckily making the ribbon (aka toolbar, first introduced in Word 2007) wo…
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This video shows where to find templates, what they are used for, and how to create and save a custom template using Microsoft Word.

911 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

20 Experts available now in Live!

Get 1:1 Help Now