I want to highlight a word if it is not followed by another specific word using VB

橙三吉。 提交于 2020-12-15 05:35:12

问题


So I'm a total newbie when it comes to using VB. I am trying to highlight a word when it is not followed by another specific word within the next two words. I tried the following code but it seems to just the first word. Many thanks in advance.

    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

回答1:


The code below should do what you want. You need to bear in mind that what Word defines as a Word can be different to what a human would, e.g. an IP address is counted as 7 words!

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



回答2:


The following would probably be significantly faster if there are many 'n.' strings in the document:

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


来源:https://stackoverflow.com/questions/64353326/i-want-to-highlight-a-word-if-it-is-not-followed-by-another-specific-word-using

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!