[英]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中的工作原理:
You can determine the original colour as follows: 您可以按如下方式确定原始颜色:
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
. 作为替换术语,输入\\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代码,你应该能够将上述步骤记录为宏,然后查看生成的代码。
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.