Solved

Word VBA remove character format style

Posted on 2013-10-29
5
1,596 Views
Last Modified: 2013-10-30
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
Scrap.docx
0
Comment
Question by:JohnRobinAllen
  • 3
5 Comments
 
LVL 8

Expert Comment

by:5teveo
ID: 39609700
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

Loop
0
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 250 total points
ID: 39609823
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

0
 

Author Comment

by:JohnRobinAllen
ID: 39610079
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
           Selection.Font.Reset
     Loop
     
     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
0
 

Author Comment

by:JohnRobinAllen
ID: 39610085
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.
     JRA
0
 

Author Closing Comment

by:JohnRobinAllen
ID: 39611421
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
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Introduction This tutorial provides instructions on how to properly format your Word document using the inbuilt tools provided. The benefits of using these tools means your documents are more accessible and easily portable to other applications an…
I would like to show you some basics you can do with Mailings in MS Word. It´s quite handy feature you can use for creating envelopes, labels, personalized letters etc. First question could be what is this feature good for? Mailing can really he…
This video shows where to find the word count, how to display it, and what it breaks down to in Microsoft Word.
This Micro Tutorial well show you how to find and replace special characters in Microsoft Word. This is similar to carriage returns to convert columns of values from Microsoft Excel into comma separated lists.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now