【问题标题】:I want to highlight a word if it is not followed by another specific word using VB如果它后面没有使用VB的另一个特定单词,我想突出显示一个单词
【发布时间】:2020-10-17 11:15:42
【问题描述】:

所以在使用 VB 时,我完全是新手。当接下来的两个单词中没有另一个特定单词时,我试图突出显示一个单词。我尝试了以下代码,但它似乎只是第一个词。非常感谢。

    Sub fek()
'
' 
'
'
 Selection.Find.ClearFormatting
    With Selection.Find
        .Text = "n."
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
    End With
    Selection.Find.Execute

    If Selection.Find.Found = True Then
        With Selection.Range
        
        .MoveStart wdWord, 2
        
        End With
        
        With Selection.Find
        .Text = "fek"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = True
        .MatchWholeWord = False
        .MatchWildcards = False
        .MatchSoundsLike = False
        .MatchAllWordForms = False
                    End With
                    
                    End If
                    
        If Selection.Find.Found = False Then
        Selection.Range.HighlightColorIndex = wdYellow
            End If
End Sub

【问题讨论】:

    标签: vba ms-office word


    【解决方案1】:

    下面的代码应该做你想做的事。您需要记住,Word 定义为 Word 的内容可能与人类的定义不同,例如一个IP地址算7个字!

    Sub fek()
       Dim findRange As Range
       Dim nextWords As Range
       
       Set findRange = ActiveDocument.Content
       With findRange.Find
          .ClearFormatting
          .Text = "n."
          .Forward = True
          .Wrap = wdFindStop
          .Format = False
          .MatchCase = True
          .MatchWholeWord = False
          .MatchWildcards = False
          .MatchSoundsLike = False
          .MatchAllWordForms = False
       
          Do While .Execute = True
             'findRange is now the range of the match so set nextWords to the 2 next words
             Set nextWords = findRange.Next(wdWord)
             nextWords.MoveEnd wdWord, 3
             'look for the specific text in the next two words
             If InStr(nextWords.Text, "fek") = 0 Then findRange.HighlightColorIndex = wdYellow
             'collapse and move findRange to the end of the match
             findRange.Collapse wdCollapseEnd
             findRange.Move wdWord, 4
          Loop
       End With
    End Sub
    

    【讨论】:

    • 只需将nextWords.MoveEnd wdWord 更改为nextWords.MoveEnd wdWord, 3
    • 感谢您的回复。该代码似乎最初可以工作,但似乎无论 fek 单词是否在附近,它都会突出显示该单词。我根据您所说的关于单词如何计算单词的内容扩大了范围,但似乎仍然没有区别。我在希腊语中使用它——我不知道这是否是个问题,但它适用于其他代码片段。
    • 请查看编辑后的答案。我最初误解了您想要的内容,当我意识到您只想在以下单词 不存在 出现的情况下突出显示初始单词时,我迅速颠倒了逻辑,忘记了 InStr 不会评估为真假。我的答案中的代码在我的测试中有效,尽管我没有在希腊文本上测试它。
    • 这在希腊语中也很完美。我真的很感激!
    【解决方案2】:

    如果有很多“n”,以下可能会明显更快。文档中的字符串:

    Sub Demo()
    Application.ScreenUpdating = False
    Dim i As Long
    i = Options.DefaultHighlightColorIndex
    Options.DefaultHighlightColorIndex = wdYellow
    With ActiveDocument.Range
      With .Find
        .Forward = True
        .Format = False
        .MatchCase = False
        .Wrap = wdFindContinue
        .MatchWildcards = True
        .ClearFormatting
        .Replacement.ClearFormatting
        .Replacement.Highlight = True
        .Text = "n."
        .Replacement.Text = "^&"
        .Execute Replace:=wdReplaceAll
        .Replacement.Highlight = False
        .Text = "n.[^s ]@fek"
        .Execute Replace:=wdReplaceAll
        .Text = "n.[^s ]@[!^s ]@fek"
        .Execute Replace:=wdReplaceAll
        .Text = "n.[^s ]<[!^s ]@>[^s ]@fek"
        .Execute Replace:=wdReplaceAll
        .Text = "n.[^s ]<[!^s ]@>[^s ]@[!^s ]@fek"
        .Execute Replace:=wdReplaceAll
      End With
    End With
    Options.DefaultHighlightColorIndex = i
    Application.ScreenUpdating = True
    End Sub
    

    【讨论】:

      猜你喜欢
      • 2013-05-19
      • 1970-01-01
      • 2013-02-17
      • 2010-12-22
      • 2010-09-12
      • 1970-01-01
      • 2014-02-15
      • 1970-01-01
      相关资源
      最近更新 更多