Hi Vivka, aaaah yes that was a combination I didn't think of so thank you for that. I have now further updated the code which removes any white space after paragraph marks and before paragraph marks and also the removal of any empty paragraphs. At the end of the code I've had to include removing a tab after an opening double quote - not sure if you would have done something different with my additions. Thank you so much for your patience with me in getting this code to work as I needed it to, I really appreciate the help you have provided.
Code:
Sub FormatManualNumbering()
Dim rng As Range
Dim rngEnd As Long
On Error GoTo Err_Handler:
Application.ScreenUpdating = False
Set rng = ActiveDocument.Range
With ActiveDocument.Range
.InsertBefore vbCr
With .Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchWildcards = False
'Removes leading spaces at beginning of paragraphs
.text = "^p^w"
.Replacement.text = "^p"
.Execute Replace:=wdReplaceAll
.text = "^w^p"
.Execute Replace:=wdReplaceAll
End With
.Characters.First.text = vbNullString
End With
Set rng = ActiveDocument.Range
With rng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchWildcards = True
'Remove empty paras:
.text = "^13{2,}"
.Replacement.text = "^p"
.Execute Replace:=wdReplaceAll
End With
Set rng = ActiveDocument.Range
'Insert a para sign at the doc's start to include the doc's 1st para in work:
rng.Characters.First.InsertBefore Chr(13)
rngEnd = rng.End
With rng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchWildcards = True
'Code_1. Insert a tab before any 1st letter in a para:
.text = "(^13[!^13]@)([A-Za-z])"
.Replacement.text = "\1^t\2"
.Execute Replace:=wdReplaceAll
'Code_2. Replace two tabs with one tab:
.text = "^t^t"
.Replacement.text = "^t"
.Execute Replace:=wdReplaceAll
'Code_3. Delete tabs between letters, which have been inserted by Code_1:
.text = "([A-Za-z])^t([A-Za-z])"
.Replacement.text = "\1\2"
.Execute Replace:=wdReplaceAll
End With
'Running the next Find-Replace causes run-time error 5623
'(The Replace With text contains a group number which is out of range)
'which may occur because the parameters of Find-Replace may be kept in the next use of Find-Replace.
'Resetting rng is one of the ways to correct the error:
Set rng = ActiveDocument.Range
Do
With rng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchWildcards = True
'Find a str between a tab & the nearest previous para sign, i.e.
'a str between a para sign & a tab, excluding other paras in-between:
.text = "^13[!^13]@^t"
If .Execute And rng.End <= rngEnd Then
.text = "[,:; ]"
.Replacement.text = "."
.Execute Replace:=wdReplaceAll
Else: Exit Do
End If
'Delete all periods immediately before a tab:
While rng.Characters.Last.Previous = "."
rng.Characters.Last.Previous.Delete
Wend
rng.Collapse wdCollapseEnd
End With
Loop
'Reset rng (see the comment above):
Set rng = ActiveDocument.Range
With rng.Find
.ClearFormatting
.Replacement.ClearFormatting
.Forward = True
.Wrap = wdFindStop
.Format = True
.MatchWildcards = True
'Insert periods after lone 1st-level numberings:
.text = "(^13[0-9]{1,})^t"
.Replacement.text = "\1.^t"
.Execute Replace:=wdReplaceAll
'Delete extra periods in the doc:
.text = "[.]{2,}"
.Replacement.text = "."
.Execute Replace:=wdReplaceAll
'Delete tab after opening bracket:
.text = "(^13[(])^t"
.Replacement.text = "\1"
.Execute Replace:=wdReplaceAll
'Delete tab after opening double quote after para marks:
.text = "(" & Chr(34) & ")^t([!^13])"
.Replacement.text = "\1\2"
.Execute Replace:=wdReplaceAll
End With
'Delete the doc's starting para sign inserted previously:
ActiveDocument.Range.Characters.First.Delete
Application.ScreenUpdating = True
Set rng = Nothing
Err_Handler:
End Sub