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

╄→гoц情女王★ 提交于 2019-12-18 16:57:13

问题


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 printouts. Manually selecting and changing will take me hours.


回答1:


Following on from stakx’s Word 97 solution, here’s what works in Word 2010:

  1. Open the Find and Replace dialogue (e.g. Ctrl-H)
  2. Click in the "Find what" box.
  3. Format drop-down, Font, choose the Font color to be found, OK.
  4. Click in the "Replace with" box.
  5. Format drop-down, Font, choose the colour to end up with, OK.
  6. Observe that the Format: description for "Find what" and "Replace with" is now different.
  7. Replace/Replace All/Find Next as desired.

You can determine the original colour as follows:

  1. Click on a bit of text with the original colour
  2. Open the colour palette. If neither a "Theme color" nor a "Standard color" is selected, you may need to click on "More colors".



回答2:


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.

Steps 2, 3 and 5 (entering search and replace regular expressions) may not actually be necessary.

If you definitely need VBA code, you should be able to record the above steps as a macro and then look at the generated code.




回答3:


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


来源:https://stackoverflow.com/questions/3691154/find-all-instances-of-yellow-text-and-change-font-color-to-red

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!