?
Solved

VBA code to assign shortcut key in Word 2007

Posted on 2008-10-26
5
Medium Priority
?
1,452 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

This article describes how to use the Send to Mail Recipient command. The instructions apply generally to Office 2007 and later versions, but Microsoft® Word 2013 was used for the specific steps and figures.  What is Send to Mail Recipient? Send…
You need to know the location of the Office templates folder, so that when you create new templates, they are saved to that location, and thus are available for selection when creating new documents.  The steps to find the Templates folder path are …
Learn how to make your own table of contents in Microsoft Word using paragraph styles and the automatic table of contents tool. We'll be using the paragraph styles in Word’s Home toolbar to help you create a table of contents. Type out your initial …
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.
Suggested Courses

764 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