Search code examples
vbams-wordms-office

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


So after Timothy Rylatt helped me with this question I am now having another problem. I only want to highlight the first word if the word that follows is numerical. I have tried to do so but it does not seem to be working. Any help would be appreciated and apologies if I am making any obvious errors I am (VERY) new to 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

Solution

  • I tried this which seems to be working except for the fact that using a parenthesis or a quote sign do not work in my string but if I use a word instead it works fine:

    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