Consulting

Results 1 to 2 of 2

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

Threaded View

Previous Post Previous Post   Next Post Next Post
  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

Posting Permissions

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