【问题标题】:How to Select all Highlights of a certain color in a Word document如何在Word文档中选择某种颜色的所有高光
【发布时间】:2020-05-18 05:12:20
【问题描述】:

我需要一次选择 .docx 文件中的所有绿色突出显示。我认为它是必要的宏,但我不知道如何编译它。有人可以帮我解决这个问题吗?

我找到了这段代码,但是找到它们后它会删除所有高亮,而我只需要选择它们:

    Sub Highlight()
Dim r As Range
Set r = ActiveDocument.Range

With r.Find
.Highlight = True
Do While .Execute(FindText:="", Forward:=True) = True
If r.HighlightColorIndex = wdBrightGreen Then
r.HighlightColorIndex = wdAuto
r.Collapse 0
End If
Loop
End With
End Sub

【问题讨论】:

  • 替换r.HighlightColorIndex = wdAuto r.Collapse 0 byr.select
  • 致Dorian:这只会选择文档的最后一个突出显示,而我需要选择它的所有绿色突出显示
  • 你到底想做什么
  • 一次选择文档的所有绿色突出显示的文本
  • 为了做什么?

标签: vba ms-word highlight


【解决方案1】:

VBA 世界中有很多不同的颜色索引。您需要定义要使用的确切绿色。这可能是一个很好的起点。

Sub DeleteYellowHighlight()
Dim rDcm As Range
Set rDcm = ActiveDocument.Range
With rDcm.Find
    .Highlight = True
While .Execute
If rDcm.Font.ColorIndex = 4 Then
    rDcm.Select
End If
Wend
End With
End Sub

或者……

Sub FindNextYellow()
With Selection.Find
    .ClearFormatting
    .Replacement.ClearFormatting
    .Text = ""
    .MatchWildcards = False
    .Forward = True
    .Wrap = wdFindContinue
    .Highlight = True
    Do
        .Execute
    Loop Until Selection.Range.Font.ColorIndex = 4 _
      Or Not .Found
    Selection.Range.Select
End With
End Sub

此外,请参阅此链接,了解您可以做什么以及在哪里可以使用这种东西。

https://www.excel-pratique.com/en/vba/colors

【讨论】:

  • 是的,在 Word 中运行。打开 Word 文档后,按 ALT+F11(同时)查看 VBA 窗口;单击插入 > 模块。将您的代码粘贴到该窗口中。按 F8 一次单步执行一行代码;按 F5 运行代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-08-16
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-03-26
  • 2019-10-01
相关资源
最近更新 更多