views:

72

answers:

2

Hi, I need a vba macro that searches for all text that has font colour as yellow within a MS Word 2007 document and changes it to red. The yellow color won't show in the printouts. Manually selecting and changing will take me hours :( Thanks.

A: 
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
Otaku
A: 

There's actually a non-programming solution for this. I've tried it in Word 97, so I'd assume Word 2007 would still allows this:

  1. Open the Search & Replace dialog.

  2. Tick the checkbox that says, Pattern search (or similar).

  3. As search term, enter (?).

  4. Select a formatting for the search (yellow text color).

  5. As replacement term, enter \1.

  6. Select the formatting for the replacement (red text color).

  7. Then search and replace everything.

stakx