突出显示另一个 Word 或 Excel 文档中的短语列表中的短语

问题描述

在这链接中找到了这个宏,效果很好,https://wordribbon.tips.net/T001173_Highlight_Words_from_a_Word_List.html

它突出显示当前文档中另一个 Word 文档中的单词。我需要看看它是否适用于短语而不仅仅是单个单词。如果我需要在阶段前后放置识别标记,例如双括号或 [[ ... ]] 之类的东西,也没关系。这可能吗?如何将它放在这个宏中?

如果列表可以来自 Excel 文档,那就更好了,但不会破坏交易。

例如: Tony the Tiger(但只有当宏找到整个短语时。按照现在的工作原理,它会独立地找到所有三个单词的所有实例,“the”当然会有问题。另一个是“17th c”。在在这种情况下,它还会找到每个 c 和每个点。最好只找到整个短语。

解决方法

您发布的链接中的代码效率很低。有关更有效的方法,请参阅:

https://www.msofficeforums.com/word-vba/23196-need-help-creating-macro.html

一个不需要其他文件的简单解决方案是:

YTD_Sales = CALCULATE(SUM('sales'[invoice_amount]),'sales'[invoice_date].[Year] = 2021)

或者,如果要突出显示的字符串列表是固定的:

Sub BulkHighlighter() Application.ScreenUpdating = False Dim i As Long,StrFnd As String,HiLt As Long HiLt = Options.DefaultHighlightColorIndex Options.DefaultHighlightColorIndex = wdBrightGreen StrFnd = "敏捷的棕色狐狸|跳过|懒惰的狗" 使用 ActiveDocument.Range.Find .ClearFormatting .Replacement.ClearFormatting .MatchWholeWord = 真 .MatchCase = 假 .Replacement.Highlight = True .Replacement.Text = "^&" 对于 i = 0 到 UBound(Split(StrFnd,"|")) .Text = Split(StrFnd,"|")(i) .执行替换:=wdReplaceAll 下一个 结束于 Options.DefaultHighlightColorIndex = HiLt Application.ScreenUpdating = True 结束子

如果您坚持使用 Excel 文件,请尝试:

Sub BulkHighlighter()
Application.ScreenUpdating = False
Dim i As Long,HiLt As Long
HiLt = Options.DefaultHighlightColorIndex
Options.DefaultHighlightColorIndex = wdBrightGreen
StrFnd = InputBox("Insert your 'Find' terms with | delimiters,for example:" & vbCr & "the quick brown fox|jumped over|the lazy dog")
With ActiveDocument.Range.Find
  .ClearFormatting
  .Replacement.ClearFormatting
  .MatchWholeWord = True
  .MatchCase = False
  .Replacement.Highlight = True
  .Replacement.Text = "^&"
  For i = 0 To UBound(Split(StrFnd,"|"))
    .Text = Split(StrFnd,"|")(i)
    .Execute Replace:=wdReplaceAll
  Next
End With
Options.DefaultHighlightColorIndex = HiLt
Application.ScreenUpdating = True
End Sub

要使用 Excel 工作簿处理同一文件夹中的多个文档,请参阅:

https://www.msofficeforums.com/70404-post4.html

请注意,最后一个链接中的代码还显示了如何处理页眉、页脚等内容。