I first posted the issue in VBA Express several months ago but haven't had a reply to that so am posting here in the hope I can resolve some of the issues within the code. I hope I haven't broken any etiquette rules by doing so.
HTML Code:
http://www.vbaexpress.com/forum/showthread.php?69330-Word-VBA-help-with-missing-punctuation-macro&highlight=
Document with comments.DOCX
I need some help to iron out a few issues with the code. The code crashes when there are comments at the end of a paragraph with no punctuation. How can I tell the code to skip/ignore comments altogether.
I have tried adding in Chr(160) to include if the semi-colon is followed by a non-breaking space instead of a space but it didn't work and now sure how to include for the code to look for both a space and/or a non-breaking space after a semi-colon.
Code:
oRng.MoveStartWhile Chr(32), wdBackward
There is a list of words that the code should ignore if the para ends with a semi-colon and any of the words, but the code isn't recognising 'and/or' and is still being highlighted. Is this because it is separated with a forward slash and therefore the code isn't recognising it?
Code:
Case "and", "but", "or", "then", "and/or", "plus", "minus", "less", "nor"
I would also like the code to ignore footnotes if they appear at the end of a paragraph as currently they are all being highlighted and not sure what to add so the code skips/ignore footnotes and also square brackets whether they appear in text form fields or not.
Code:
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", "plus", "minus", "less", "nor"
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
End Sub