我想突出显示一个词,如果它后面没有使用 VB 的另一个特定词 [英] I want to highlight a word if it is not followed by another specific word using VB

查看:42
本文介绍了我想突出显示一个词,如果它后面没有使用 VB 的另一个特定词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

推荐答案

下面的代码应该可以满足您的需求.您需要记住,Word 定义为 Word 的内容可能与人类不同,例如一个IP地址算7个字!

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

这篇关于我想突出显示一个词,如果它后面没有使用 VB 的另一个特定词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆