Hi Andrew, so I've had a go at trying to update the code - the original code doesn't remove any spaces or non breaking spaces from the end of the paragraphs first, so when running the code its not inserting any punctuation at all - I've added some code to try and delete the space or non breaking space but I'm getting an error message at oRng.text = "" saying range cannot be deleted.
I'm also a bit stumped on how to change the comma or space before the string sLastWord to a semi colon - the string also needs to find instances of 'and/or' - in code terms would this be classed as 3 words or 6 characters?
If you could help at all I would be really grateful - thanks
Code:
Sub AddPuncDemo3()
Dim Para As Paragraph, oRng As Range, sLastWord As String, sFirstChar As String
'Application.ScreenUpdating = False
For Each Para In ActiveDocument.Paragraphs
With Para.Range
Set oRng = Para.Range
oRng.End = oRng.End - 1
oRng.Collapse 0
oRng.MoveStartWhile Chr(32), wdBackward 'Space
oRng.MoveStartWhile Chr(160), wdBackward 'Non breaking space
oRng.text = "" 'ERROR SAYING RANGE CANNOT BE DELETED
If .Information(wdInFieldResult) Or .Information(wdWithInTable) Or .Font.AllCaps Or .Font.Bold Or Len(.text) < 3 Then
GoTo NextFor
Else
Do While .Characters.Last.Previous = " "
.Characters.Last.Previous.Delete
Loop
sFirstChar = .Characters(1)
sLastWord = .Words.Last.Previous.Words(1)
Debug.Print sFirstChar, sLastWord
If Not sLastWord Like "*[.!?:;,]" Then 'If para ends with any of these characters do nothing
Select Case sLastWord
Case "and", "but", "or", "and/or", "then", "plus", "minus", "less", "nor"
'do nothing
'ADD IN HERE TO CHANGE COMMA OR SPACE BEFORE THESE WORDS TO SEMI COLON IF ONE IS NOT ALREADY THERE
Set oRng = .Words.Last.Previous.Words(1)
oRng.MoveStartWhile Chr(32), wdBackward 'Space
oRng.MoveStartWhile Chr(160), wdBackward 'Non breaking space
oRng.Start = oRng.Start - 1
If oRng.Characters(1) = ";" Then
'do nothing
If oRng.Characters(1) = "," Or " " Then
'change comma to semi colon or add semi colon if space
.Characters.Last.InsertBefore ";"
Case Else
If sFirstChar = UCase(sFirstChar) Then
.Characters.Last.InsertBefore "." 'Insert period if para starts Uppercase
Else
.Characters.Last.InsertBefore ";" 'Insert semi colon if para starts lowercase
End If
End Select
End If
End If
End With
NextFor:
Next
'Application.ScreenUpdating = True
End Sub