vba 查找黄色文本的所有实例并将字体颜色更改为红色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3691154/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Find all instances of yellow text and change font color to red
提问by Raaskull
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.
我需要一个 vba 宏来搜索 MS Word 2007 文档中所有字体颜色为黄色的文本并将其更改为红色。黄色不会显示在打印输出中。手动选择和更改将花费我几个小时。
回答by Colin Frame
Following on from stakx's Word 97 solution, here's what works in Word 2010:
继 stakx 的 Word 97 解决方案之后,以下是 Word 2010 中的工作:
- Open the Find and Replace dialogue (e.g. Ctrl-H)
- Click in the "Find what" box.
- Format drop-down, Font, choose the Font color to be found, OK.
- Click in the "Replace with" box.
- Format drop-down, Font, choose the colour to end up with, OK.
- Observe that the Format: description for "Find what" and "Replace with" is now different.
- Replace/Replace All/Find Next as desired.
- 打开查找和替换对话框(例如 Ctrl-H)
- 单击“查找内容”框。
- 格式下拉,字体,选择要查找的字体颜色,确定。
- 单击“替换为”框。
- 格式下拉,字体,选择最后的颜色,确定。
- 请注意,“查找内容”和“替换为”的格式:描述现在不同了。
- 根据需要替换/全部替换/查找下一个。
You can determine the original colour as follows:
您可以按如下方式确定原始颜色:
- Click on a bit of text with the original colour
- Open the colour palette. If neither a "Theme color" nor a "Standard color" is selected, you may need to click on "More colors".
- 单击具有原始颜色的一些文本
- 打开调色板。如果既没有选择“主题颜色”也没有选择“标准颜色”,您可能需要点击“更多颜色”。
回答by stakx - no longer contributing
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 仍然允许这样做:
- Open the Search & Replacedialog.
- Tick the checkbox that says, Pattern search(or similar).
- As search term, enter
(?). - Select a formatting for the search (yellow text color).
- As replacement term, enter
\1. - Select the formatting for the replacement (red text color).
- Then search and replace everything.
- 打开搜索和替换对话框。
- 勾选显示模式搜索(或类似)的复选框。
- 作为搜索词,输入
(?)。 - 选择搜索格式(黄色文本颜色)。
- 作为替换项,输入
\1。 - 选择替换的格式(红色文本颜色)。
- 然后搜索并替换所有内容。
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代码,您应该能够将上述步骤记录为宏,然后查看生成的代码。
回答by Todd Main
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

