【问题标题】:Microsoft Word Macro for highlighting multiple words用于突出显示多个单词的 Microsoft Word 宏
【发布时间】:2018-09-06 05:28:15
【问题描述】:

我的意图是创建一个非常基本的宏来查找一系列单词并突出显示它们。不幸的是,我不知道如何一步完成多个单词。例如,以下代码有效:

Sub Macro1()
'
' Macro1 Macro
'
'
    Selection.Find.ClearFormatting
    Selection.Find.Replacement.ClearFormatting
    Selection.Find.Replacement.Highlight = True
    With Selection.Find
        .Text = "MJ:"
        .Replacement.Text = ""
        .Forward = True
        .Wrap = wdFindContinue
        .Format = True
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute Replace:=wdReplaceAll
End Sub

但是,如果我添加另一个 .Text = 行,则 MJ: 将被忽略。有什么想法吗?

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    如果您只查找几个单词,只需在同一个宏中执行多个查找和替换即可完成您想要的操作。例如,以下将以黄色突出显示所有出现的“target1”和“target2”

    Sub HighlightTargets()
    
    ' --------CODE TO HIGHLIGHT TARGET 1-------------------
        Options.DefaultHighlightColorIndex = wdYellow
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        Selection.Find.Replacement.Highlight = True
        With Selection.Find
            .Text = "target1"
            .Replacement.Text = "target1"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    
    ' --------CODE TO HIGHLIGHT TARGET 1-------------------
        Options.DefaultHighlightColorIndex = wdYellow
        Selection.Find.ClearFormatting
        Selection.Find.Replacement.ClearFormatting
        Selection.Find.Replacement.Highlight = True
        With Selection.Find
            .Text = "target2"
            .Replacement.Text = "target2"
            .Forward = True
            .Wrap = wdFindContinue
            .Format = True
            .MatchCase = False
            .MatchWholeWord = False
            .MatchWildcards = False
            .MatchSoundsLike = False
            .MatchAllWordForms = False
        End With
        Selection.Find.Execute Replace:=wdReplaceAll
    End Sub
    

    或者,下面的代码可以让您在一行中添加所有要突出显示的术语,这可能更容易使用。

    Sub HighlightTargets2()
    
    Dim range As range
    Dim i As Long
    Dim TargetList
    
    TargetList = Array("target1", "target2", "target3") ' put list of terms to find here
    
    For i = 0 To UBound(TargetList)
    
    Set range = ActiveDocument.range
    
    With range.Find
    .Text = TargetList(i)
    .Format = True
    .MatchCase = True
    .MatchWholeWord = False
    .MatchWildcards = False
    .MatchSoundsLike = False
    .MatchAllWordForms = False
    
    Do While .Execute(Forward:=True) = True
    range.HighlightColorIndex = wdYellow
    
    Loop
    
    End With
    Next
    
    End Sub
    

    【讨论】:

      【解决方案2】:

      我做了以下修改。也许没有数组那么优雅。但我的想法是按照用户简单地将值列表粘贴到字段中的方式。

      Sub HighlightKeyword(SearchWord As String)
      '
      ' HighlightKeyword Macro
      '
          Options.DefaultHighlightColorIndex = wdYellow
          Selection.Find.ClearFormatting
          Selection.Find.Replacement.ClearFormatting
          Selection.Find.Replacement.Highlight = True
          With Selection.Find
              .Text = SearchWord
              .Replacement.Text = ""
              .Forward = True
              .Wrap = wdFindContinue
              .Format = True
              .MatchCase = True
              .MatchWholeWord = False
              .MatchWildcards = False
              .MatchSoundsLike = False
              .MatchAllWordForms = False
          End With
          Selection.Find.Execute Replace:=wdReplaceAll
      End Sub
      
      Sub HighlightKeywordList()
      '
      ' HighlightKeywordList 
      '
      '
          Dim HighlightList As String
          Dim WordList As Variant
      
          HighlightList = "Lorem Ipsum,amit,Finibus,Bonorum,et Malorum,Vestibulum,Vivamus,Integer"
          WordList = Split(HighlightList, ",")
      
          For i = 0 To UBound(WordList)
      
              HighlightKeyword (WordList(i))
          Next i
      
      End Sub
      

      【讨论】:

        猜你喜欢
        • 2013-08-20
        • 2012-01-26
        • 2017-08-30
        • 1970-01-01
        • 2015-11-13
        • 1970-01-01
        • 1970-01-01
        • 2011-07-12
        • 1970-01-01
        相关资源
        最近更新 更多