JoshG
JoshG

Reputation: 145

Deleting Empty Paragraphs in Word Using VBA: Not All Empty Paragraphs Deleted

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

Upvotes: 0

Views: 6825

Answers (2)

Slai
Slai

Reputation: 22876

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 

Upvotes: 1

Wayne G. Dunn
Wayne G. Dunn

Reputation: 4312

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

Upvotes: 6

Related Questions