Word and VBA: Formatting text inside a character style

    In a different question, I wondered whether it would be possible select some formatted text and apply a character format style to it without destroying the formatting. In comment 3644204, Graham Skan correctly assured me that it would be impossible to do so.
     I subsequently looked at some documents that I had massaged with some long-since lost code, and the documents show formatted text with a character style applied to them. I use the character style to hide or show certain words. For example, the attached document, an excerpt from one of my old documents, hides or shows a comment. Shift + Alt + J hides the blue text that has a character formatting of “LB Mont Comment”. Shift + Alt + Y exposes the “LB Mont Comment” text. The text that is either displayed or hidden has some italics.
     Apparently there is a way to have text formatted accomplish that through code. I know that if some words already have a specific character style, one can format the words manually without losing the character style. I suppose that before I have the computer apply a character style to some words, I can have it search for things like italics, underlined text, or boldface text, and have the code put unique marks at the start and end of such areas. Then after I subsequently apply the character style that destroys formatting, I could have the computer search for those markings and reformat the text according to what is needed. To avoid having the computer mark other formatted text in the document, I think I would have to make a new document, cut and paste the text to be reformatted into the new document, make my style marking there, and then copy and paste the marked text back into the original document, apply the character style to it, and then apply the formatting as per what markings the text has.
     I suspect there might be an easier way to do the same thing, but if necessary, I will do it the hard way. However, I would appreciate some suggestions as to how those tasks could be done more easily.

JohnRobinAllenRetired professor of FrenchAsked:
Who is Participating?
Stacy BrownConnect With a Mentor Senior Applications AdministratorCommented:
Selection.Font.Reset is what you are looking for, I think.  You don't need to copy text out of a document to apply a character style.  Sometimes it is wise to reset the font formatting though.  Selection.Font.Reset is the same as selecting text and hitting CTRL+Spacebar to remove any Character formatting.

You can also do a find/replace to apply formatting.  See the code below.

'This finds the word Lorem and formats it with the Character style I named Lorem Bold Italics

    Selection.Find.Replacement.Style = ActiveDocument.Styles( _
        "Lorem Bold Italics")
    With Selection.Find
        .Text = "Lorem "
        .Replacement.Text = "^&"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = False
        .MatchWholeWord = False
        .MatchByte = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = False
        .MatchFuzzy = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

Open in new window

JohnRobinAllenConnect With a Mentor Retired professor of FrenchAuthor Commented:
My basic problem was how to attach a character style to text without changing the formatting already on the text. There appears to be no way to do that, but there is a way to record aspects of the style, apply the character formatting that destroys the formatting, and then go back and reformat the text as recorded. In a subsequent comment that I will post to this question after I return in a couple of weeks, I will include the code that does that. Here is a description of what the code does:

We first find six odd characters that do not appear in the text. These will mark the starting points and ending points of text formatted as italics, underlining, and boldface. We then go through the text that will soon have a character format style and search for those three styles, to mark the locations with those unique characters. We then apply the character style, and finally go through the text to reformat specified areas and delete the special marks.

It looks like a clumsy kludge to a problem, but it works very quickly and does the job. Finalword’s solution does not solve that problem, but the solution I wrote uses some of Finalword’s  ideas, so he should get half the credit with my thanks.
JohnRobinAllenRetired professor of FrenchAuthor Commented:
I will later send the code that does the work.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.