0
votes

I wrote a macro to delete all the empty paragraphs in my document, but it exhibits weird behavior: If there are a number of empty paragraphs at the very end of the document, about half of them are deleted. Repeatedly running the macro gradually eliminates the empty paragraphs until only one empty paragraph remains. Even if there is a boundary condition so that I need a line of code to delete the last paragraph, but I still don't understand why only half of the empty paragraphs at the end are deleted. Can anyone explain why this is happening and how to correct this behavior? As an aside, I searched online and saw numerous posts about detecting paragraph markers (^p, ^13, and others, but only searching vbCr worked, which is another minor puzzle.)

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
It's because you are going from top down instead from bottom up -- or your code needs to not increment the paragraph if a delete occurs. Imagine ten blank paragraphs at the top. First gets deleted, making #2 now #1. But your pointer now says #2... see the problem?Wayne G. Dunn
I've already accounted for the need to not increment, by using the num_of_deleted_paras variable. Also, if there's a string of empty paragraphs in the middle of the document, there doesn't seem to be a problem; it's specifically at the end that poses the problem--and some of them actually get deleted!JoshG
You can take a look at similar code I wrote that would delete blank paragraphs at the TOP of pages. All you would need to do is remove the 'Else Exit For' and that should work -- assuming the other items are ok (blank pages, etc): See: stackoverflow.com/questions/42659628/…Wayne G. Dunn
If your first para is blank, then second iteration of your for loop is 3 > no_of _total paras. You are skipping para 2 from processing.cyboashu
By the way use Find to delete paras, its way faster than looping into paragraphs.cyboashu

2 Answers

5
votes

This code will delete all blank paragraphs...

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
1
votes

You can replace the paragraph marks:

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