G
Greg
The other day I noticed that Find ^13{2,} and Replace with ^p failed to
remove empty paragraphs located between tables. Dave Rado in his
article
http://word.mvps.org/FAQs/MacrosVBA/DeleteEmptyParas.htm
implies the above find and replace pattern will remove all empty PMs
except the emtpy PM immediately preceeeding and following a table. I
have modified a macro that Dave posted in that article for removing
"all" empty PMs. The code is posted below for review and comment.
Sub RemoveEmptyPMs()
Dim oRng As Word.Range
Dim oTable As Table
Dim oCell As Cell
Dim Counter As Integer
Dim MyRange As Range
Dim emptyPara As Boolean
Dim EPFirstAndLast As Range
Set oRng = ActiveDocument.Content
'Remove empty PMs general
With oRng.Find
.Text = "^13{2,}"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
For Each oTable In oRng.Tables
#If VBA6 Then
'For Word 2000 and higher for speed
oTable.AllowAutoFit = False
#End If
'Remove empty PMs in table cells
Set oCell = oTable.Range.Cells(1)
For Counter = 1 To oTable.Range.Cells.Count
If Len(oCell.Range.Text) > 2 And _
oCell.Range.Characters(1).Text = vbCr Then
oCell.Range.Characters(1).Delete
End If
If Len(oCell.Range.Text) > 2 And _
Asc(Right$(oCell.Range.Text, 3)) = 13 Then
Set MyRange = oCell.Range
MyRange.MoveEnd Unit:=wdCharacter, Count:=-1
MyRange.Characters.Last.Delete
End If
Set oCell = oCell.Next
Next Counter
'Remove empty PMs immediate before, after, and between tables
Set MyRange = oTable.Range
MyRange.Collapse wdCollapseEnd
If MyRange.Paragraphs(1).Range.Text = vbCr Then
MyRange.Collapse wdCollapseEnd
MyRange.Move wdParagraph, 1
If MyRange.Information(wdWithInTable) Then
'Do nothing. Issue will be resolve while processing next table.
Else
MyRange.Move wdParagraph, -1
MyRange.Paragraphs(1).Range.Delete
End If
End If
Set MyRange = oTable.Range
Do
MyRange.Collapse wdCollapseStart
MyRange.Move wdParagraph, -1
If MyRange.Paragraphs(1).Range.Text = vbCr Then
MyRange.Collapse wdCollapseStart
If MyRange.Start = oRng.Start Then
MyRange.Paragraphs(1).Range.Delete
Else
MyRange.Move wdParagraph, -1
If MyRange.Information(wdWithInTable) Then
If MsgBox("You have two tables separatated" _
& " by a single empty paragraph" _
& " mark. Do you want to delete" _
& " the empty paragraph and merge" _
& " the two tables?", vbYesNo) = vbYes Then
MyRange.Move wdParagraph, 1
emptyPara = True
MyRange.Paragraphs(1).Range.Delete
End If
Else
MyRange.Move wdParagraph, 1
emptyPara = True
MyRange.Paragraphs(1).Range.Delete
End If
End If
Else
emptyPara = False
End If
Loop While emptyPara = True
Next oTable
'Remove first and last empty PM
If oRng.Paragraphs.Count > 1 Then
Set EPFirstAndLast = oRng.Paragraphs.First.Range
If EPFirstAndLast.Text = vbCr Then EPFirstAndLast.Delete
Set EPFirstAndLast = oRng.Paragraphs.Last.Range
If EPFirstAndLast.Text = vbCr Then EPFirstAndLast.Delete
End If
End Sub
remove empty paragraphs located between tables. Dave Rado in his
article
http://word.mvps.org/FAQs/MacrosVBA/DeleteEmptyParas.htm
implies the above find and replace pattern will remove all empty PMs
except the emtpy PM immediately preceeeding and following a table. I
have modified a macro that Dave posted in that article for removing
"all" empty PMs. The code is posted below for review and comment.
Sub RemoveEmptyPMs()
Dim oRng As Word.Range
Dim oTable As Table
Dim oCell As Cell
Dim Counter As Integer
Dim MyRange As Range
Dim emptyPara As Boolean
Dim EPFirstAndLast As Range
Set oRng = ActiveDocument.Content
'Remove empty PMs general
With oRng.Find
.Text = "^13{2,}"
.Replacement.Text = "^p"
.Forward = True
.Wrap = wdFindContinue
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
For Each oTable In oRng.Tables
#If VBA6 Then
'For Word 2000 and higher for speed
oTable.AllowAutoFit = False
#End If
'Remove empty PMs in table cells
Set oCell = oTable.Range.Cells(1)
For Counter = 1 To oTable.Range.Cells.Count
If Len(oCell.Range.Text) > 2 And _
oCell.Range.Characters(1).Text = vbCr Then
oCell.Range.Characters(1).Delete
End If
If Len(oCell.Range.Text) > 2 And _
Asc(Right$(oCell.Range.Text, 3)) = 13 Then
Set MyRange = oCell.Range
MyRange.MoveEnd Unit:=wdCharacter, Count:=-1
MyRange.Characters.Last.Delete
End If
Set oCell = oCell.Next
Next Counter
'Remove empty PMs immediate before, after, and between tables
Set MyRange = oTable.Range
MyRange.Collapse wdCollapseEnd
If MyRange.Paragraphs(1).Range.Text = vbCr Then
MyRange.Collapse wdCollapseEnd
MyRange.Move wdParagraph, 1
If MyRange.Information(wdWithInTable) Then
'Do nothing. Issue will be resolve while processing next table.
Else
MyRange.Move wdParagraph, -1
MyRange.Paragraphs(1).Range.Delete
End If
End If
Set MyRange = oTable.Range
Do
MyRange.Collapse wdCollapseStart
MyRange.Move wdParagraph, -1
If MyRange.Paragraphs(1).Range.Text = vbCr Then
MyRange.Collapse wdCollapseStart
If MyRange.Start = oRng.Start Then
MyRange.Paragraphs(1).Range.Delete
Else
MyRange.Move wdParagraph, -1
If MyRange.Information(wdWithInTable) Then
If MsgBox("You have two tables separatated" _
& " by a single empty paragraph" _
& " mark. Do you want to delete" _
& " the empty paragraph and merge" _
& " the two tables?", vbYesNo) = vbYes Then
MyRange.Move wdParagraph, 1
emptyPara = True
MyRange.Paragraphs(1).Range.Delete
End If
Else
MyRange.Move wdParagraph, 1
emptyPara = True
MyRange.Paragraphs(1).Range.Delete
End If
End If
Else
emptyPara = False
End If
Loop While emptyPara = True
Next oTable
'Remove first and last empty PM
If oRng.Paragraphs.Count > 1 Then
Set EPFirstAndLast = oRng.Paragraphs.First.Range
If EPFirstAndLast.Text = vbCr Then EPFirstAndLast.Delete
Set EPFirstAndLast = oRng.Paragraphs.Last.Range
If EPFirstAndLast.Text = vbCr Then EPFirstAndLast.Delete
End If
End Sub