Wednesday, November 5, 2014

Character Styles Revisited

I wish that more MS Word professionals would make using character styles, not manual formatting, the way they provide help. I just read this Word Tip and, in light of what I wrote about the other day, I'm shaking my head.
In the text below, I've added comments in bold/italic to make my point.

It is not uncommon to use repeating design elements in a document. For instance, you may want all occurrences of a particular word to appear in bold italics, at a certain point size. While you can certainly do the formatting by hand, it is much more efficient to allow a macro to do the work for you. Actually, it's more efficient to define a character style that can store the formatting based upon the function that the certain word performs within the document. By handling the formatting in this way, you don't need to worry about remembering how the word should appear. And you wouldn't have to worry about remembering how the word should appear if you were to set up a character style. This macro, FormatWords, is an example of such a macro.
Sub FormatWords()
   Selection.Find.ClearFormatting
   Selection.Find.Replacement.ClearFormatting
   With Selection.Find
        .Text = "Warning!"
        .Replacement.Text = "" 
        .Replacement.Font.Bold = True No! Use .Replacement.Style.Warning
        .Replacement.Font.Italic = True If you use the above line,                                         you don't need this line.
        .Forward = True
        .Wrap = wdFindContinue
        .MatchWholeWord = True
   End With
   Selection.Find.Execute Replace:=wdReplaceAll
End Sub
When you run this macro, it searches for all occurrences of the word Warning (followed by an exclamation point) and changes the formatting on it so the word is bold and italics text to use a character style called Warning that can then be applied not only to the document you are working with, but any other documents that you write, assuming you are using the same template.

The only reason this tip came to my attention is that I have an email alert set up to receive emails when there is a new comment. On November 3, 2014, there was this comment left:

Rasmus     03 Nov 2014, 07:34
Hi.
Can I use this macro to make several different words in bold?
Best Regards Rasmus

This is what I was going to post as a comment on the site, but it turned into a longer "comment" than I wanted.

Yes, you can use this macro to replace several words. Personally, I would not use this macro as written. I would define a character style called "Warning" and define it as bold / italic. Then I would change the macro code. Instead of defining the 'replacement' values as bold and italic, I would replace it with the defined character style. In this way, if you use the same template for multiple documents, you can control the formatting across all of those documents with a character style.

Manually replacing "Warning" with bold and italic will put a Band-Aid on the document and get you what you want, but you should definitely consider whether you want to have a character style to control the formatting.

To answer your question, you can change the .Text line and the formatting you want to apply, as shown below:
---------------------------------------------------------------------------------------------------------
    .Text = "Warning!" <== Change this word
    .Replacement.Text = "" <== don't change this line
    .Replacement.Font.Bold = True <== change this line 
    .Replacement.Font.Italic = True <== change this line 
---------------------------------------------------------------------------------------------------------
For example, if you wanted to replace "Warning!" with bold/italic as well as "Note:" with bold, the macro would look like this:
Sub FormatWords()
   Selection.Find.ClearFormatting
   Selection.Find.Replacement.ClearFormatting
   With Selection.Find
        .Text = "Warning!"
        .Replacement.Text = ""
        .Replacement.Font.Bold = True
        .Replacement.Font.Italic = True
        .Forward = True
        .Wrap = wdFindContinue
        .MatchWholeWord = True
   End With
   Selection.Find.Execute Replace:=wdReplaceAll
   Selection.Find.ClearFormatting
   Selection.Find.Replacement.ClearFormatting
   With Selection.Find
        .Text = "Note:"
        .Replacement.Text = ""
        .Replacement.Font.Bold = True
        .Forward = True
        .Wrap = wdFindContinue
        .MatchWholeWord = True
   End With
   Selection.Find.Execute Replace:=wdReplaceAll
End Sub
------------------------------------------------------------------------------------------------------------------
Again, I would define a character style called "Warning" and a character style called "Note" and use those character styles instead of manual formatting.

Using character style to control formatting is the only way I ever want to work in MS Word.

No comments:

Raining