How to code Word 2007 shortcut keys using VBA

C

CISadmin

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

Thanks in advance,
 
C

Cindy M.

Hi =?Utf-8?B?Q0lTYWRtaW4=?=,
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.
Could you please be more precise by what you mean when you say it's
"auto-loaded from Add-in"?

Is the macro saved in the document, or its template, or a global
template?

Have you discovered the KeyBindings part of the Word object model?
That's where you'd need to start.

Also, don't forget to specify the CustomizationContext (where you want
to save the keyboard assignment).

Cindy Meister
INTER-Solutions, Switzerland
http://homepage.swissonline.ch/cindymeister (last update Jun 17 2005)
http://www.word.mvps.org

This reply is posted in the Newsgroup; please post any follow question
or reply in the newsgroup and not by e-mail :)
 
C

CISadmin

Thanks Cindy,

I have used the below VBA code which is working fine.

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top