Style Macros for technical authors working in Word - ISerializable - Roy Osherove's Blog

Style Macros for technical authors working in Word

One of the things you do as an author of a book working in MS Word, is do a lot of formatting on the text that you write. When working with the manning word templates, for example, you are expected to use a list of built in text styles that manning created, with names such as ".body" and "code" for formatting different elements in your text.

 

It's quite tedious to use the mouse to set styles on text selections, and as a keyboard junky it bothered me that you can't have specific shortcuts to set the style of the selection.

A solution to this can be made by creating custom macros in word, each setting the selection to a different style, and then attaching a special keyboard shortcut to each of these macros.

this process in itself is quite tedious and time consuming, but I've done it. now you don't have to. I've even automated the creation of the special keybaord shortcuts.

Steps to make:

  1. In word, press ALT-F11 or open the macros editor
  2. Paste in the code for the macros in VBA as shown below.
  3. Customize the styles names that each "formatXX" method sets to match the styles you'd like.
  4. Customize the letters for the shortcuts as you wish in the "InstallAllMacros" method (it's Ctrl-Alt-[letter])
  5. Put the cursor inside the method named "InstallAllMacros" and press F5 to run it. when it has finished running you will have shortcuts as written in the method for the specific styles. The shortcuts are all in the form of Ctrl+Alt+[letter] which is shown in the method you just ran.
  6. You can now get back to work.

VBA Code for settings styles automatically:

Sub InstallAllMacros()
    Call InstallMacro("FormatBody", wdKeyB)
    Call InstallMacro("FormatCodeChar", wdKeyH)
    Call InstallMacro("FormatCode", wdKeyC)
    Call InstallMacro("FormatHeader1", wdKey1)
    Call InstallMacro("FormatHeader2", wdKey2)
    Call InstallMacro("FormatHeader3", wdKey3)
    Call InstallMacro("FormatFigure", wdKeyF)
End Sub

Sub FormatCodeChar()
    Call FormatSel(".Code Char")
End Sub
Sub FormatFigure()
    Call FormatSel(".Figure")
End Sub

Sub FormatHeader1()
    Call FormatSel(".Head 1")
End Sub

Sub FormatHeader2()
    Call FormatSel(".Head 2")
End Sub

Sub FormatHeader3()
    Call FormatSel(".Head 3")
End Sub

Sub FormatCode()
    Call FormatSel(".Code")
    Selection.ParagraphFormat.Alignment = wdAlignParagraphLeftEnd
End Sub
Sub FormatBody()
    Call FormatSel(".Body")
End Sub

Sub InstallMacro(CommandName As String, Key1 As WdKey)
    CustomizationContext = NormalTemplate
    KeyBindings.Add wdKeyCategoryCommand, CommandName, BuildKeyCode(Key1, wdKeyAlt, wdKeyControl)
End Sub

Sub FormatSel(StyleName As String)
    Selection.Style = ActiveDocument.Styles(StyleName)
End Sub

 

Have fun. Hope this saves you as much time as it did for me.

Published Monday, November 26, 2007 6:19 PM by RoyOsherove
Filed under:

Comments

Tuesday, November 27, 2007 1:21 AM by Ken Egozi

# re: Style Macros for technical authors working in Word

Are doing the outlining for the book yourself?

I thought you are sending them text files, and they'd have a freehand/whatever dude for that

Tuesday, November 27, 2007 7:00 AM by RoyOsherove

# re: Style Macros for technical authors working in Word

Ken: I do the basic styling, and that is then used to generate some of the book's layout. the styling are "hints" to the automated processing and pre-production software.

Tuesday, November 27, 2007 7:24 AM by Steve

# re: Style Macros for technical authors working in Word

Roy, I believe you can already do this from within Word.

Goto...

Modify style > Format > Shortcut Key.

Tuesday, November 27, 2007 7:49 AM by RoyOsherove

# re: Style Macros for technical authors working in Word

Steve, can you list the exact steps in Office 2007?

I can't find it.

Tuesday, November 27, 2007 7:54 AM by RoyOsherove

# re: Style Macros for technical authors working in Word

OK, I found it.

- press the big round icon on the top left

- press word options

- select customize on the left

- select "keyboard shortcuts: customize" button on the bottom middle

- in the categories scroll all the way down to "styles"

- select the desired style to apply on the right

- hit the desired shortcut inside the "Press new shortcut key" field

- press "assign" on the bottom left

Amazingly intuitive.

Thanks for letting me know!

Tuesday, November 27, 2007 8:02 AM by Steve

# re: Style Macros for technical authors working in Word

The way I tend to do it in 2007 is...

- Select the Home tab in the ribbon

- Click the arrow in the bottom right of the 'Styles' browser

- A panel should popup to the right of the document pane

- Right click a style, select Modify

- Then select Format > Shortcut Key

Neither was is exactly intuitive though!

Tuesday, November 27, 2007 8:09 AM by Ian Griffiths

# re: Style Macros for technical authors working in Word

There's a less convoluted path.

1. Right-click on the style.  (Do this either in the Styles gallery in the Home ribbon, or on the Styles tool window if you have that open.)

2. Select Modify... from the context menu

3. Click on Format...

4. Select Shortcut Key... from the menu

5. and 6. same as your last two steps.

Step 3. is pretty counter-intuitive - what does a shorcut key have to do with a style's formatting? But that aside, this has always seemed like a much more direct approach to me. And it's a couple of steps shorter than the route you found.