Thread: [Solved] VBA IF Statement Help
View Single Post
 
Old 03-15-2024, 08:54 AM
gmaxey gmaxey is offline Windows 10 Office 2019
Expert
 
Join Date: May 2010
Location: Brasstown, NC
Posts: 1,601
gmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nicegmaxey is just really nice
Default

Shelley,


If you pull that loop out and run it on your test document, you will see where and why the error occurs:

Code:
Sub A()
Dim oPara As Paragraph
Dim Rng As Range
  On Error GoTo Err_Handler:
For Each oPara In ActiveDocument.Paragraphs 'Highlights missing punctuation at end of paragraphs
    With oPara.Range
    If .Information(wdWithInTable) Or .Font.AllCaps Or .Characters.First.Font.Bold Or Len(.Text) < 3 Then
        GoTo NextFor
      Else
      If Not .Characters.Last.Previous Like "*[.!?:;,]" Then
      If Not .Characters.Last.Previous.Fields(1).Result = "]" Then
      If Not .Characters.Last.Previous Like "]" Then
      .Words.Last.Previous.Words(1).HighlightColorIndex = wdPink
       End If
       End If
       End If
       
      Select Case .Words.Last.Previous.Words(1)
      Case "and", "but", "or", "then", "plus", "minus", "less", "nor"
      Set Rng = .Words.Last    '.Previous.Words(1)
      Rng.MoveStartUntil cSet:=" " & Chr(160), Count:=-10
      Set Rng = Rng.Characters.First.Previous.Previous
      If Rng.Text = ";" Then
      'if oPara ends with these words and have semi-colon before them do nothing no highlight else
      .Words.Last.Previous.Words(1).HighlightColorIndex = wdNoHighlight
      If Rng.Text = "," Then
      'if oPara ends with these words and have semi-colon before them do nothing no highlight else
      .Words.Last.Previous.Words(1).HighlightColorIndex = wdPink
      End If
      End If
      Case Else
      End Select
      End If
      End With
NextFor:
  Next
lbl_Exit:
  Exit Sub
Err_Handler:
  MsgBox Err.Number & " - " & Err.Description
  oPara.Range.Select
  Resume
End Sub

There is no field(1) in the range.
__________________
Greg Maxey
Please visit my web site at http://www.gregmaxey.com/
Reply With Quote