Skip to content

Instantly share code, notes, and snippets.

@tdalon
Created September 30, 2016 06:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdalon/2817ced08d066cee0b0a821ec0b897b0 to your computer and use it in GitHub Desktop.
Save tdalon/2817ced08d066cee0b0a821ec0b897b0 to your computer and use it in GitHub Desktop.
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment