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
以下代码在运行两次时会执行所需的操作。替换功能本身无法处理这个问题,永远也不能。
请注意 MVP 页面中有关删除所有空段落的警告,这将合并仅由段落返回分隔的两个表。
Graham Mayor从论坛安装宏