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