tdalon
9/30/2016 - 6:24 AM

MS Office Word VBA Macros to delete empty paragraphs

MS Office Word VBA Macros to delete empty paragraphs

Sub DeleteEmptyParagraphs()
    Dim oPara As Word.Paragraph
    For Each oPara In ActiveDocument.Paragraphs
        If Len(oPara.Range) = 1 Then oPara.Range.Delete
    Next
End Sub

Sub DeleteEmptyParagraphsWithSpaces()
    Dim oPara As Word.Paragraph
    Dim var
    Dim SpaceCounter As Long
    Dim oChar As Word.Characters
    For Each oPara In ActiveDocument.Paragraphs
        If Len(oPara.Range) = 1 Then
            oPara.Range.Delete
        Else
            SpaceCounter = 0
            Set oChar = oPara.Range.Characters
            For var = 1 To oChar.Count
                If Asc(oChar(var)) = 32 Then
                    SpaceCounter = SpaceCounter + 1
                End If
            Next
            If SpaceCounter + 1 = Len(oPara.Range) Then
                 ' paragraph contains ONLY spaces
                oPara.Range.Delete
            End If
        End If
    Next
End Sub