【问题标题】:Deleting Empty Paragraphs in Word Using VBA: Not All Empty Paragraphs Deleted使用 VBA 删除 Word 中的空段落:并非所有空段落都已删除
【发布时间】:2017-11-15 20:52:10
【问题描述】:

我编写了一个宏来删除文档中的所有空段落,但它表现出奇怪的行为:如果文档末尾有许多空段落,则其中大约一半会被删除。反复运行宏逐渐消除空段落,直到只剩下一个空段落。即使有边界条件需要一行代码删除最后一段,但我还是不明白为什么最后只删除了一半的空段。谁能解释为什么会发生这种情况以及如何纠正这种行为?顺便说一句,我在网上搜索了很多关于检测段落标记的帖子(^p、^13 等,但只有搜索 vbCr 有效,这是另一个小难题。)

Sub Delete_Empty__Paras_2() 'This macro looks for empty paragraphs and deletes them.
Dim original_num_of_paras_in_doc As Integer
Dim num_of_deleted_paras As Integer

original_num_of_paras_in_doc = ActiveDocument.Paragraphs.Count 'Count the number of paragraphs in the document to start
num_of_deleted_paras = 0 'In the beginning, no paragraphs have been deleted

Selection.HomeKey Unit:=wdStory 'Go to the beginning of the document.
For current_para_number = 1 To original_num_of_paras_in_doc 'Process each paragraph in the document, one by one.
    If current_para_number + num_of_deleted_paras > original_num_of_paras_in_doc Then 'Stop processing paragraphs when the loop has processed every paragraph.
        Exit For
    Else 'If the system just deleted the 3rd paragraph of the document because
        ' it's empty, the next paragraph processed is the 3rd one again,
        'so when we iterate the counter, we have to subtract the number of deleted paragraphs to account for this.
        Set paraRange = ActiveDocument.Paragraphs(current_para_number - num_of_deleted_paras).Range
        paratext = paraRange.Text
        If paratext = vbCr Then 'Is the paragraph empty? (By the way, checking for vbCr is the only method that worked for checking for empty paras.)
            paratext = "" 'Delete the paragraph.
            ActiveDocument.Paragraphs(current_para_number - num_of_deleted_paras).Range.Text = paratext
            num_of_deleted_paras = num_of_deleted_paras + 1 'Iterate the count of deleted paras.
        End If
    End If
Next current_para_number
End Sub

【问题讨论】:

  • 这是因为您是自上而下而不是自下而上 - 或者如果发生删除,您的代码不需要增加段落。想象一下顶部有十个空白段落。首先被删除,使#2 现在成为#1。但是您的指针现在显示 #2... 看到问题了吗?
  • 我已经通过使用 num_of_deleted_pa​​ras 变量考虑了不增加的需要。另外,如果文档中间有一串空段落,似乎没有问题;尤其是在最后提出了问题 - 其中一些实际上被删除了!
  • 您可以查看我编写的类似代码,该代码将删除页面顶部的空白段落。您需要做的就是删除“Else Exit For”,这应该可以工作——假设其他项目都可以(空白页等):请参阅:stackoverflow.com/questions/42659628/…
  • 如果你的第一个段落是空白的,那么你的 for 循环的第二次迭代是 3 > no_of _total paras。您正在从处理中跳过第 2 段。
  • 顺便用Find删除段落,比循环进入段落要快。

标签: vba ms-word


【解决方案1】:

此代码将删除所有空白段落...

Sub RemoveBlankParas()
    Dim oDoc        As Word.Document
    Dim i           As Long
    Dim oRng        As Range
    Dim lParas      As Long

    Set oDoc = ActiveDocument
    lParas = oDoc.Paragraphs.Count          ' Total paragraph count
    Set oRng = ActiveDocument.Range

    For i = lParas To 1 Step -1
        oRng.Select
        lEnd = lEnd + oRng.Paragraphs.Count                         ' Keep track of how many processed
        If Len(ActiveDocument.Paragraphs(i).Range.Text) = 1 Then
            ActiveDocument.Paragraphs(i).Range.Delete
        End If
    Next i

    Set para = Nothing
    Set oDoc = Nothing
    Exit Sub
End Sub

【讨论】:

  • 也许 OP 的问题与仅包含空格的段落有关。如果您在检查 Text 的长度之前对其进行 LTrim,您的代码也会将其删除。
【解决方案2】:

您可以替换段落标记:

ActiveDocument.Range.Find.Execute FindText:="^p^p", ReplaceWith:="^p", Replace:=wdReplaceAll

ActiveDocument.Range.Find.Execute "^p^p", , , , , , , , , "^p", wdReplaceAll ' might be needed more than once 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-12-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-01
    • 2015-05-11
    • 2020-10-08
    相关资源
    最近更新 更多