Find all instances of yellow text and change font color to red

前端 未结 3 1471
野趣味
野趣味 2021-01-03 01:47

I need a vba macro that searches for all text that has font color as yellow within a MS Word 2007 document and changes it to red. The yellow color won\'t show in the printou

3条回答
  •  感情败类
    2021-01-03 02:18

    Sub ChangeColorWithReplace()   
        Selection.Find.ClearFormatting
        Selection.Find.Font.Color = wdColorYellow
        Selection.Find.Replacement.ClearFormatting
        Selection.Find.Replacement.Font.Color = wdColorRed
        With Selection.Find
            .Text = ""
            .Replacement.Text = ""
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchKashida = False
            .MatchDiacritics = False
            .MatchAlefHamza = False
            .MatchControl = False
            .MatchByte = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    End Sub
    

    Also, here's another way to do it. It's not extremely fast, but it's faster than doing it manually:

    Sub ChangeFontColorByCharacter()
        Application.ScreenUpdating = False
        Dim d As Document: Set d = ActiveDocument
        For i = 1 To d.Characters.Count
            If d.Characters(i).Font.TextColor.RGB = RGB(255, 255, 0) Then
                d.Characters(i).Font.TextColor.RGB = RGB(255, 0, 0)
                DoEvents
            End If
        Next
        Application.ScreenUpdating = True
    End Sub
    

提交回复
热议问题