Word VBA help with missing punctuation macro
Hi this is my first time posting to this forum so hope I'm doing this correctly. I have a macro that highlights where punctuation is missing at the end of paragraphs:
1. It doesn't work if the paragraph ends in a cross reference field.
2. If sublevel paragraphs end with a comma before "and", "but", "or", "then" to also be highlighted pink BUT the sublevels end with a semi-colon before "and", "but", "or", "then" then these paragraphs should not be highlighted as the correct punctuation is in place.
Can anyone please help me fine tune the code below. Much appreciated.
Code:
Sub DPU_HighlightMissingPunctuation()Application.ScreenUpdating = False
Dim Para As Paragraph, oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = "([!^13.,:;\?\-\!]^13)" 'Looks for missing punctuation at end of paragraphs
.Font.Bold = False
.Replacement.Highlight = wdPink 'and highlight last character as pink
.Execute Replace:=wdReplaceAll 'BUT DOESN'T WORK IF LAST CHARACTER IS A FIELD
End With
On Error Resume Next
For Each Para In ActiveDocument.Paragraphs
With Para.Range
If Len(.Text) > 2 Then
If Not .Characters.Last.Previous Like "[.!?:;]" Then 'if para ends with punctuation do not highlight
Select Case .Words.Last.Previous.Words(1)
Case "and", "but", "or", "then" 'if para ends with these words and have semi-colon before them do nothing no highlight else
.HighlightColorIndex = wdNoHighlight
'do nothing
Case Else
Case "and", "but", "or", "then" 'if para ends with these words and have a comma before them highlight pink
.Characters.Last.InsertBefore ","
.HighlightColorIndex = wdPink
End Select
End If
End If
End With
Next
Application.ScreenUpdating = True
End Sub
1 Attachment(s)
VBA help with punctuation macro
The issue seems to be when the comment is at the end of the paragraph where punctuation is missing and this is what is crashing the code - how can I tell the code to skip comments? I've added a test document so you can see. Thanks