如何检查下一个单词是否为数字以确定动作

How can I check if the next word is numerical to determine action

因此,在 Timothy Rylatt 帮助我解决 问题后,我现在遇到了另一个问题。如果后面的单词是数字,我只想突出显示第一个单词。我试过这样做,但似乎没有用。如果我犯了任何明显的错误,我将不胜感激并道歉 我是 VB.

的(非常)新手
Sub feknew()
   Dim findRange As Range
   Dim nextWords As Range
   Dim NumChk  As Range
   
   Set findRange = ActiveDocument.Content
   With findRange.Find
      .ClearFormatting
      .Text = "í. "
      .Forward = True
      .Wrap = wdFindStop
      .Format = False
      .MatchCase = True
      .MatchWholeWord = False
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
      
      Set NumChk = findRange.Next(wdWord)
      If IsNumeric(NumChk) Then
      
                                   
         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, "á") = 0 Then findRange.HighlightColorIndex = wdYellow
         'collapse and move findRange to the end of the match
         findRange.Collapse wdCollapseEnd
         findRange.Move wdWord, 4
      
      Loop
         End If
         End With
         End Sub

它不起作用,因为你在错误的地方添加了数字测试。此时 Find 尚未执行, findRange 指向整个文档。测试需要在循环内,就像在您正在编辑的例程中一样。

因为你只需要检查下一个词,所以不需要引入新的变量,NumChk

例程也应该更通用一些,这样您就不需要为要突出显示的每个字符串都对其进行编辑。

Sub UsageExample()
   HighlightIfFollowedByNumber "text"
End Sub

Sub HighlightIfFollowedByNumber(textToFind As String)
   Dim findRange As Range
   
   Set findRange = ActiveDocument.Content
   With findRange.Find
      .ClearFormatting
      .Text = textToFind
      .Forward = True
      .Wrap = wdFindStop
      .Format = False
      .MatchCase = True
      .MatchWholeWord = False
      .MatchWildcards = False
      .MatchSoundsLike = False
      .MatchAllWordForms = False
      
      Do While .Execute = True
         If IsNumeric(findRange.Next(wdWord).Text) Then findRange.HighlightColorIndex = wdYellow
         findRange.Collapse wdCollapseEnd
         findRange.Move wdWord
      Loop
   End With
End Sub

这真的很简单——你甚至不需要宏:

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 = "í.[^s ]@[0-9,.]{1,}>"
    .Replacement.Text = "^&"
    .Execute Replace:=wdReplaceAll
  End With
End With
Options.DefaultHighlightColorIndex = i
Application.ScreenUpdating = True
End Sub

宏也不需要循环。

我尝试了这个似乎有效,除了使用括号或引号在我的字符串中不起作用但如果我使用单词代替它工作正常:

Sub feknew()
     Dim findRange As Range
     Dim nextWords As Range
     Dim textToFind As String
        
   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
         If IsNumeric(findRange.Next(wdWord).Text) Then
         Set nextWords = findRange.Next(wdWord)
         nextWords.MoveEnd wdWord, 4
         'look for the specific text in the next two words
        If InStr(nextWords.Text, "(A'") = 0 Then findRange.HighlightColorIndex = wdYellow
         'collapse and move findRange to the end of the match
         findRange.Collapse wdCollapseEnd
         findRange.Move wdWord, 4

      End If
      Loop
   End With