简体   繁体   English

查找黄色文本的所有实例并将字体颜色更改为红色

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

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. 我需要一个vba宏来搜索MS Word 2007文档中字体颜色为黄色的所有文本并将其更改为红色。 The yellow color won't show in the printouts. 打印输出中不会显示黄色。 Manually selecting and changing will take me hours. 手动选择和更改将花费我几个小时。

Following on from stakx's Word 97 solution, here's what works in Word 2010: 继stakx的Word 97解决方案之后,以下是Word 2010中的工作原理:

  1. Open the Find and Replace dialogue (eg Ctrl-H) 打开“查找和替换”对话框(例如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". 如果既未选择“主题颜色”也未选择“标准颜色”,则可能需要单击“更多颜色”。

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: 我在Word 97中尝试过,所以我假设Word 2007仍然允许这样:

  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 . 作为替换术语,输入\\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. 可能实际上不需要步骤2,3和5(输入搜索和替换正则表达式)。

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. 如果你肯定需要VBA代码,你应该能够将上述步骤记录为宏,然后查看生成的代码。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM