Hi Andrew, I've just got one last thing I need to change so that the code runs correctly and then I can mark this post as complete. I've spent hours googling for the answer but I just can't find it.
The code needs to look to see if there is already punctuation before the last word/character e.g. square bracket and if there is go to next. I think I need to change this line:
Code:
If sLastWord = "]" Or sLastWord = ")" Then sLastWord = .Words.Last.Previous.Previous.Words(1)
Code:
If sLastWord = ")" Then sLastWord = .Words.Last.Previous.Previous.Words(1) 'Looks for punc after a bracket
If sLastWord = "]" Then sLastWord = .Words.Last.Previous.Previous.Words(1) 'Looks for punc before a square bracket
I've tried many different combinations of the .Words.Last.Previous.Previous.Words(1) but I just can't seem to get it to look before the last character.
I've added comments to the code so I can learn what each line does but there are a few I don't know.
Code:
Sub AddPuncDemo6()
Dim Para As Paragraph, oRng As Range, sLastWord As String, sFirstChar As String, bListEnd As Boolean, i As Long, rngSearch As Range
Dim Toc As Boolean
'Application.ScreenUpdating = False
With ActiveDocument.Range.Find
.ClearFormatting
.text = "^w^p" 'clears white space at end of paras
.Replacement.text = "^p"
.Execute Replace:=wdReplaceAll
End With
For Each Para In ActiveDocument.Paragraphs
With Para.Range
.Select
If .Information(wdWithInTable) Or .Font.AllCaps Or .Characters.First.Font.Bold Or Len(.text) < 3 Then 'WHAT DOES LEN(.TEXT) >3 MEAN?
GoTo NextFor
Else
sFirstChar = .Characters(1) 'Look for first character at beginning of paras
If sFirstChar = "[" Or sFirstChar = "(" Then sFirstChar = .Characters(2) 'Look for second character after bracket/square bracket at beginning of para
sLastWord = .Words.Last.Previous.Words(1) 'Look for last character of para
'If sLastWord = "]" Or sLastWord = ")" Then sLastWord = .Words.Last.Previous.Previous.Words(1) 'If last character is a square bracket or bracket
If sLastWord = ")" Then sLastWord = .Words.Last.Previous.Previous.Words(1) 'Looks for punc after a bracket
If sLastWord = "]" Then sLastWord = .Words.Last.Previous.Previous.Words(1) 'Looks for punc before a square bracket
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", "then", "plus", "minus", "less", "nor" 'Sub paras ending with these words look for punc before them
Set oRng = .Words.Last '.Previous.Words(1)
oRng.MoveStartUntil cSet:=" ", count:=-10 'If characters are less than 10
Set oRng = oRng.Characters.First.Previous.Previous
oRng.Select
If oRng.text = "," Then 'Change comma before string words to semi-colon
oRng.text = ";" 'Add semi-colon before string words
ElseIf oRng.text Like "[a-z0-9)]*" Or oRng.text = "]" Then 'Look for paras ending with square bracket before string words
oRng.Collapse Direction:=wdCollapseEnd
oRng.text = ";" 'Add semi-colon
End If
Case Else
If sFirstChar = UCase(sFirstChar) Then 'If first character is uppercase
.Characters.Last.InsertBefore "." 'Insert period if para starts Uppercase
Else
If Para.Range.End < ActiveDocument.Range.End Then 'WHAT DOES THIS MEAN?
bListEnd = Para.Range.ParagraphFormat.OutlineLevel > Para.Next.Range.ParagraphFormat.OutlineLevel 'Looks for outline levels
If bListEnd Then
.Characters.Last.InsertBefore "." 'Insert period if stepping up levels
Else
.Characters.Last.InsertBefore ";" 'Insert semi colon followed by same or lower level
End If
Else
.Characters.Last.InsertBefore "." 'Insert period if para starts Uppercase
End If
End If
End Select
End If
End If
End With
NextFor:
Next
'Application.ScreenUpdating = True
MsgBox "Complete"
End Sub