Hi, I have been working with the code below (created by Graham M) on legal documents for a while now but today have come across an issue - if there are comments within the document that appear in a window to the right, the code crashes Word - I've tested the same document without the comments and it works just fine - what could I add to the code to deal with comments - also by way of interest, the code highlights the whole word where punctuation is missing but I would like it to only highlight the last letter if possible.
Sub HighlightMissingPunctuation()Dim oPara As Paragraph
Dim oRng As Range
Application.ScreenUpdating = False
On Error Resume Next
For Each oPara In ActiveDocument.Paragraphs
With oPara.Range
Set oRng = oPara.Range
oRng.End = oRng.End - 1
oRng.Collapse 0
oRng.MoveStartWhile Chr(32), wdBackward
oRng.text = ""
If .Characters.Last.Previous.InRange(ActiveDocument.TablesOfContents(1).Range) = False Then
If oPara.Range.Information(wdWithInTable) = False Then
If Len(.text) > 2 And Not .Font.Bold And Not .Font.AllCaps 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", "and/or"
Set oRng = .Words.Last.Previous.Words(1)
oRng.MoveStartWhile Chr(32), wdBackward
oRng.Start = oRng.Start - 1
If oRng.Characters(1) = ";" 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
End If
If oRng.Characters(1) = "," Then
'if oPara ends with these words and have comma before them highlight pink
.Words.Last.Previous.Words(1).HighlightColorIndex = wdPink
End If
Case Else
End Select
End If
End With
Next oPara
Application.ScreenUpdating = True
lbl_Exit:
Set oPara = Nothing
Set oRng = Nothing
Exit Sub
End Sub