Character Styles Macro

March 26, 2003 – 12:00 pm

Most Microsoft Word users who need to use bold or italic just press CTRL + B or CTRL + I and go blithely on their way, not thinking any more about it. But at some point, they’ll run into problems. For example, their directly applied formatting may disappear when they apply a paragraph style over the top of it. You can read more about this here:

http://www.topica.com/lists/editorium/read/message.html?mid=1709281006

Another example is that sometimes directly applied formatting simply refuses to be found with Word’s Find feature. I don’t know why that is, but I’ve seen it time after time.

The solution to such problems is to avoid using directly applied character formatting entirely. Instead, use character *styles* formatted as bold or italic. Unfortunately, doing so isn’t nearly as easy as using plain old character formatting–until now. I’m providing a macro that will create the character style you need and then toggle between bold and roman, italic and roman, or other formatting using Word’s built-in keyboard shortcuts and toolbar buttons. Pretty slick! Here’s the macro for italic:

‘MACRO STARTS HERE

Dim Found, myStyle

Found = False

For Each myStyle In ActiveDocument.Styles

If myStyle.NameLocal = “Italic” Then

Found = True

Exit For

End If

Next

If Found = False Then

ActiveDocument.Styles.Add _

Name:=”Italic”, _

Type:=wdStyleTypeCharacter

ActiveDocument.Styles(”Italic”).BaseStyle = _

“Default Paragraph Font”

ActiveDocument.Styles(”Italic”).Font.Italic = True

End If

mySel = Selection.Font.Italic

If mySel = wdUndefined Or mySel = False Then

Selection.Style = “Italic”

Else

Selection.Style = “Default Paragraph Font”

End If

‘MACRO ENDS HERE

If you don’t know how to use macros like that one, you can learn how here:

http://www.topica.com/lists/editorium/read/message.html?mid=1706922855

The first part of the macro (from “For Each” to “Next”) checks to see if the character style (in this case named “Italic”) already exists. If it does, the macro leaves it alone, which means you can create and format your character styles any way you like so they will work with this macro. If the character style *doesn’t* exist, the macro creates it with the appropriate formatting (in this case, italic–note the line that says “ActiveDocument.Styles(”Italic”).Font.Italic = True”).

The second part of the macro checks to see if any part of the selection (which may be selected text or simply the text at the cursor position) is already formatted as italic. If it’s not (or if part of it is), the macro applies the Italic character style. If the selection is already italic, the macro applies the Default Paragraph Font to make the selection roman.

You’ve probably already figured out that you can modify the macro to take care of bold, underlining, or other kinds of formatting. To do so, you’ll need to change “Italic” to “Bold” (or whatever) wherever it appears in these six lines of the macro:

If myStyle.NameLocal = “Italic” Then

Name:=”Italic”, _

ActiveDocument.Styles(”Italic”).BaseStyle = _

ActiveDocument.Styles(”Italic”).Font.Italic = True

mySel = Selection.Font.Italic

Selection.Style = “Italic”

Note that in the following line, you’ll have to change it twice:

ActiveDocument.Styles(”Italic”).Font.Italic = True

In making your changes, you can use Bold, Italic, Underline, SmallCaps, AllCaps, Superscript, Subscript, Strikethrough, Hidden, Outline, or Shadow. (A few other formats are also available; if you’re interested, see the “Properties” listing for “Font Object” in Word’s Visual Basic Help file.)

There’s one more line you might be interested in modifying:

“Default Paragraph Font”

You can change this line to the name of an actual font you want to use (for example, “Baskerville”). This is useful if you want to specify the name of a true italic font to provide italic formatting or to get fancy in other ways.

To get the macro to work when you press one of Word’s built-in keyboard commands (such as CTRL + I) or toolbar buttons, simply give the macro the same name as the Word command. For example, if you name the macro “Italic,” like this–

Sub Italic()

–then Word will happily treat it just as if it were the built-in Italic command! For your convenience, the names of Word’s built-in character formatting commands are Bold, Italic, Underline, SmallCaps, AllCaps, Superscript, Subscript, Strikethrough, Hidden, Outline, and Shadow.

After you’ve used the macro to apply formatting to some text, you’ll see the name of the character style (such as “Italic”) in the Styles list on the Formatting toolbar.

Here’s an equivalent macro for Word 6 or 95; notice that the formatting is set with “1″ or “-1″ in case you want to change it:

‘MACRO STARTS HERE

If StyleDesc$(”Italic”) = “” Then

FormatStyle .Name = “Italic”, \

.BasedOn = “Default Paragraph Font”, \

.Type = 1, .AddToTemplate = 0, .Define

FormatDefineStyleFont \

.Bold = - 1, \

.Italic = 1, \

.Underline = - 1, \

.SmallCaps = - 1, \

.AllCaps = - 1, \

.Superscript = - 1, \

.Subscript = - 1, \

.Strikethrough = - 1, \

.Hidden = - 1, \

.Outline = - 1, \

.Shadow = - 1, \

.Font = “(normal text)”

End If

If Italic() <> 1 Then

Style “Italic”

Else

Style “Default Paragraph Font”

End If

‘MACRO ENDS HERE

Thanks to Steve Hudson for VBA advice.

_________________________________________

READERS WRITE

Susan Bullowa wrote:

If you have the Styles and Formatting Task Pane open In Word 2002 and you hover the arrow cursor over the paragraph mark to the right of the style name, the tool tip with all of the style’s attributes appears. I find the tool tip information useful because it usually displays more detail than the listing of attributes in the Modify Styles dialog box. When the tool tip appears and I want to record the information, I press the Print Scr button and paste the picture into Paint. In that way, I can print the information for myself while I build my spreadsheet of style attributes.

Thanks to Susan for the useful tip.

_________________________________________

RESOURCES

Want to learn more about using styles? Check out Microsoft’s “Tips for Understanding Styles in Word”:

http://www.microsoft.com/office/using/column14.asp

_____________________________________________________

THE FINE PRINT

Editorium Update (ISSN 1534-1283) is published by:

The EDITORIUM, LLC

Microsoft Word Add-Ins for Publishing Professionals

http://www.editorium.com

Copyright © 2008 by the Editorium. All rights reserved. Editorium Update and Editorium are trademarks of the Editorium.

You may forward copies of Editorium Update to yourself and others (but not charge for it) and print or store it for your own use. Any other broadcast, publication, retransmission, copying, or storage, without written permission from the Editorium, is prohibited. Send reprint requests to reprints [at symbol] editorium.com

Editorium Update is provided for informational purposes only and without a warranty of any kind, either express or implied, including but not limited to implied warranties of merchantability, fitness for a particular purpose, and freedom from infringement. The user assumes the entire risk as to the accuracy and use of this document.

The Editorium is not affiliated with Microsoft Corporation.

_____________________________________________________

HOW TO SUBSCRIBE OR UNSUBSCRIBE

To subscribe, send a blank email message to editorium-subscribe [at symbol] topica.com.

To unsubscribe, send a blank email message to editorium-unsubscribe [at symbol] topica.com.

We do not sell, rent, or give our subscriber list to anyone.

____________________________________________________

You must be logged in to post a comment.