?
Solved

Word VBA remove character format style

Posted on 2013-10-29
5
Medium Priority
?
1,799 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 1000 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Do you ever need to create a 20 page Word document for some testing purpose? Are you tired of copying & pasting old boring "lorem ipsum" text over and over again, increasing font size and line space in order to make the document 20+ pages long? Look…
If you work with Word a lot, you probably use styles. If you use styles a lot, you've probably balled your fist more often than not when working with the ribbon. In Word 2007/2010, one of the things that I find missing when using styles is a quic…
The viewer will learn how to make their project stand out over others by learning how to change colors and shapes, add spaces, change directions, and add bullets to their charts.
In a previous video Micro Tutorial here at Experts Exchange (http://www.experts-exchange.com/videos/1358/How-to-get-a-free-trial-of-Office-365-with-the-Office-2016-desktop-applications.html), I explained how to get a free, one-month trial of Office …
Suggested Courses

801 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