Word VBA remove character format style

I have a Word document of about 200 pages in which I have designated certain words with a unique character style, “Vocab words,” that makes the words stand out in the text. (See the short excerpt attached.) I would like VBA to do a global find and replace to remove that character style on specific words. In the attached document, I would like to change “sous-officier” and “cuve” by removing the “Vocab words” style without removing the style from other words such as “cambra” in the attached document. I would like to do that with one “find and replace” command for each different word .
     Is that possible? Given the length of the text and the hundreds of words so marked, I would prefer not to have to search for each occurrence of word to make the change.
I hope there is an easy solution.
     Thanks for any suggestions
     John Robin in Priddis, Alberta
JohnRobinAllenRetired professor of FrenchAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Office 2007 macro...

This macro change formatting for 'sous-officier' text thru out document...

Selection.HomeKey Unit:=wdStory
Selection.EndKey Unit:=wdStory, Extend:=wdExtend
Do While Selection.Find.Execute(FindText:="sous-officier", Forward:=False, Format:=True) = True

    With Selection.Font
        .Name = "Times New Roman"
        .Size = 11
        .Bold = False
        .Italic = False
        .Underline = wdUnderlineSingle
        .UnderlineColor = wdColorAutomatic
        .StrikeThrough = False
        .DoubleStrikeThrough = False
        .Outline = False
        .Emboss = False
        .Shadow = False
        .Hidden = False
        .SmallCaps = False
        .AllCaps = False
        .Color = wdColorAutomatic
        .Engrave = False
        .Superscript = False
        .Subscript = False
        .Spacing = 0
        .Scaling = 100
        .Position = 0
        .Kerning = 0
        .Animation = wdAnimationNone
    End With
    Selection.Font.UnderlineColor = wdColorAutomatic
    Selection.Font.Underline = wdUnderlineNone

All text has formatting, so you have to specify a format to switch to.

Here is a macro to run the list and change the character style to another for each of the listed words:

Sub ChangeStyles()
    Dim strWords() As Variant
    Dim i As Integer
    strWords = Array("sous-officier", "cuve")
    For i = 0 To UBound(strWords)
        With ActiveDocument.Range.Find
            .Text = strWords(i)
            .Style = "Vocab words"
            .Replacement.Text = strWords(i)
            .Replacement.Style = "mw-headline"
            .Execute Replace:=wdReplaceAll
        End With
    Next i
End Sub

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
JohnRobinAllenRetired professor of FrenchAuthor Commented:
I am grateful to Steveo for his suggestion, but unfortunately his solution still leaves the “Vocab words” style attached to the changed words. The solution also makes the proposed changes individually for each instance of “sous-officier,” something I hoped I would not have to do.
     Here is the context: The first document (in French) has applied a previously-defined character style (“Vocab words”) to words that a student reading the document might not understand. All instances of any given such word have the same character style. I want to copy up to three instances of such words into another document, along with context, a few words before and after each instance of the highlighted words.
     I start at the top of the document and search for the first word with the “Vocab words” style. In the sample document, the first such word is “sous-officier.” I copy that word, its context, and location into another document. Then I search for any other instances of “sous-officier” and copy its context and location to the other document until I have up to three instances of the same word. I don’t want any more examples of the word, but the original document may have a hundred more instances of the word. My problem is how to remove the “Vocab words” style from all instances of the word in the original document so that as the program through the document to look for more such problem words it won’t “see” words already copied into the second document. I have to get rid of the “Vocab words” style attached to all the hundred or so other instances of “sous-officier.”
     An obvious but erroneous solution after I have copied three instances of the word would be to do a global find and replace to delete all instances of the word in the original document. That would give problems if a different problem word is near such a deletion: the context surrounding the different word would be corrupt.
     A kludge that I would hope to avoid would be to make a loop that would find each instance of a word, and reset the font. The following three-line kludge works but is not as fast as a global find and replace, if only that could replace the instance of the offending character style.
     Do While Selection.Find.Execute(FindText:="sous-officier") = True
     What I want may be impossible, but I thought I should check with Experts Exchange before giving up and just use the three-line kludge above.
     John Robin (Allen)
     Alberta, Canada
JohnRobinAllenRetired professor of FrenchAuthor Commented:
I wrote the above commentary on Steveo's solution before Graham Skan had posted his solution. I'll test Graham's solution by trying to apply "Normal" style to the problem words.
JohnRobinAllenRetired professor of FrenchAuthor Commented:
This is precisely the solution I was seeking. No looping through each item individually. The one line,  .Execute Replace:=wdReplaceAll, does the job changing hundreds of items at once.
     I first tried it using the underlying "Normal" paragraph style as the replacement style in line 10 of the code. That did not work. Apparently one has to use a character style to replace a character style. What a useful style is "mw-headline"!! It apparently has no characteristics but simply uses the underlying paragraph style. That is perfect for this task!
     The solution also introduced me to using "Array" as a replacement for "Split." Most of the time I'll continue to use Split since I often have arrays within arrays, each separated with different characters. Split lets me designate the delimiter for any split. "Array" saves one the trouble of specifying a comma delimeter.
     Many thanks for the solution, the essence of what is generally known as "skanian", as in "That's a skanian solution" (i.e., the best).
     John Robin  (Allen)
     Priddis, Alberta, Canada
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Word

From novice to tech pro — start learning today.