Consulting

Results 1 to 2 of 2

Thread: Loop to Find Instances of Font.Color = wdRed and Apply Custom Style

  1. #1
    VBAX Contributor
    Joined
    Aug 2012
    Posts
    120
    Location

    Loop to Find Instances of Font.Color = wdRed and Apply Custom Style

    I've got a document that has some text colored red. Sometimes it's just a word, other times a sentence or paragraph. I seem to be able to loop Find.Execute the instances of red font okay. But, I'm not getting consistent results when I try to convert those to a custom Style. It works about 50% of the time.

    Here's the code:

    Dim r As Range
    Dim Fnd As Boolean
    Fnd = True
    Set r = ActiveDocument.Content
    r.Find.ClearFormatting
    r.Find.Font.Color = wdColorRed
    Do While Fnd = True
        With r.Find
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
            .Execute
        End With
    If Fnd = True Then
            r.Select
            Selection.Style = ActiveDocument.Styles("CustTxt")
        End If
    Loop
    Last edited by Aussiebear; 06-24-2022 at 01:08 AM. Reason: Added code tags to supplied code

  2. #2
    Knowledge Base Approver VBAX Guru macropod's Avatar
    Joined
    Jul 2008
    Posts
    4,351
    Location
    There is no need for either a loop or even a macro for this. A simple Find/Replace with the appropriate colour parameter for the Find expression and the 'CustTxt' STyle specified as the Replace parameter is all you need. If you're not finding all instances that way, that suggest some of the red colouring is custom (i.e. not the standard red colour).

    Additionally, red colouring that results from Track Changes won't be affected by what you're trying to do.
    Cheers
    Paul Edstein
    [Fmr MS MVP - Word]

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •