Word Find and replace paragraph marks when there are tables
The following code, when run twice does what is needed. The replace feature, by itself, cannot handle this, never could.
Note the warnings in the MVP Page on Remove All Empty Paragraphs that this will merge two tables separated only by a paragraph return.
Sub ReplaceEmptyParagraphs()
' https://wordmvp.com/FAQs/MacrosVBA/DeleteEmptyParas.htm
' compiled from above by Charles Kenyon
' IN GENERAL - EMPTY PARAGRAPHS
With Selection.Find
.Text = "^13{2,}"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
' FIRST AND LAST EMPTY PARAGRAPHS
Dim MyRange As range
Set MyRange = ActiveDocument.Paragraphs(1).range
If MyRange.Text = vbCr Then MyRange.Delete
Set MyRange = ActiveDocument.Paragraphs.Last.range
If MyRange.Text = vbCr Then MyRange.Delete
' BEFORE AND AFTER TABLES
Dim oTable As Table
For Each oTable In ActiveDocument.Tables
#If VBA6 Then
'The following is only compiled and run if Word 2000 or 2002 is in use
'It speeds up the table and your code
oTable.AllowAutoFit = False
#End If
'Set a range to the para following the current table
Set MyRange = oTable.range
MyRange.Collapse wdCollapseEnd
'if para after table empty, delete it
If MyRange.Paragraphs(1).range.Text = vbCr Then
MyRange.Paragraphs(1).range.Delete
End If
'Set a range to the para preceding the current table
Set MyRange = oTable.range
MyRange.Collapse wdCollapseStart
MyRange.Move wdParagraph, -1
'if para before table empty, delete it
If MyRange.Paragraphs(1).range.Text = vbCr Then
MyRange.Paragraphs(1).range.Delete
End If
Next oTable
Set MyRange = Nothing
Set oTable = Nothing
End Sub
Installing macros from forums by Graham Mayor