PDA

View Full Version : Solved: Adding Styles and Shortcuts by Macro?



clhare
05-28-2008, 08:23 AM
I have a macro that adds styles to a document if they are not already there. Is there a way to also add a keyboard shortcut to the added styles using a macro?

Here's an example of the code I used to add a style:
' Create "NewStyle" Style
ActiveDocument.Styles.Add Name:="NewStyle", Type:= _
wdStyleTypeParagraph
With ActiveDocument.Styles("NewStyle")
.AutomaticallyUpdate = False
.BaseStyle = "OldStyle"
.NextParagraphStyle = "NewStyle"
End With
With ActiveDocument.Styles("NewStyle").ParagraphFormat
.SpaceBefore = 2
.SpaceAfter = 5.5
End With

fumei
05-28-2008, 12:27 PM
Easy, although....WARNING! There is no notice given if you use an existing keybinding shortcut.

Say you have Alt-3 already assigned to...well, whatever, and you execute:


CustomizationContext = NormalTemplate
KeyBindings.Add KeyCode:=BuildKeyCode(wdKey3, wdKeyAlt), _
KeyCategory:= _
wdKeyCategoryStyle, Command:="YaddaYadda"

which assigns Alt-3 to the style "YaddaYadda", it will assign it. Period. If something else had been using Alt-3, it will no longer use it. It will have NO shortcut.

This probably will not be critical for you, but if it may be, then you should test for the KeyCode you are wanting to use. See KeyCode in Help.

Essentially though, you are assigning a shortcut by:

1. setting a CustomizationContext. This can be for any template.

2. adding a KeyBinding that has a KeyCode (the keys to be used), a Category, and a Command.

For Styles (wdKeyCategoryStyle) the Command is simply the name of the style.