none
VBA Word - Empty Paragraphs Not Deleting RRS feed

  • Question

  • Hi friends,

    I have been trying to get rid of this problem for the past few hours and I cant understand what the problem is?



    How do I delete these empty paragraphs, I have tried many macros but they dont seem to work.

     Sub DeleteEmptyParagraphs()
    
        Dim oPar As Word.Paragraph
        
        Set oRng = ActiveDocument.Range
        
        For Each oPar In oRng.Paragraphs
        
        If Len(oPar.Range.Text) = 1 Then   'If the length of the paragraph is 1 character
        oPar.Range.Delete
                        
        End If
        Next oPar
     End Sub
    

    I can see a space before the paragraph mark - does that affect it?

    I simply want to condense the lines with a single paragraph at the end like normally it should be.

    I did the wild card search for  

    .Text = "[^13]{2,}"
    .Replacement.Text = "^p"

    nothing seems to work :(


    any ideas?


    Cheers Dan :)

    Wednesday, October 5, 2016 2:47 PM

Answers

  • Sub DeleteEmptyParagraphs()

        Dim oPar As Word.Paragraph
        
        Set oRng = ActiveDocument.Range
        
        For Each oPar In oRng.Paragraphs
        
        If Len(oPar.Range.Text) = 1 Then   'If the length of the paragraph is 1 character
            oPar.Range.Delete
        End If
        
        If Len(oPar.Range.Text) = 2 And Left(oPar.Range.Text, 1) = " " Then  'If the paragraph is just a space
            oPar.Range.Delete
        End If
        Next oPar
     End Sub
    • Marked as answer by Dan_CS Wednesday, October 5, 2016 3:06 PM
    Wednesday, October 5, 2016 2:58 PM

All replies

  • Sub DeleteEmptyParagraphs()

        Dim oPar As Word.Paragraph
        
        Set oRng = ActiveDocument.Range
        
        For Each oPar In oRng.Paragraphs
        
        If Len(oPar.Range.Text) = 1 Then   'If the length of the paragraph is 1 character
            oPar.Range.Delete
        End If
        
        If Len(oPar.Range.Text) = 2 And Left(oPar.Range.Text, 1) = " " Then  'If the paragraph is just a space
            oPar.Range.Delete
        End If
        Next oPar
     End Sub
    • Marked as answer by Dan_CS Wednesday, October 5, 2016 3:06 PM
    Wednesday, October 5, 2016 2:58 PM

  • Hello Bernie,

    thank you for assisting.

    I am really dissapointed at the amount of time wasted trying to delete some empty paragraphs, it must be a problem with Word 16, or my very limited knowledge.

    I did exactly as they show find the  ^p^p and replace with a single ^p,

    Then i tried to code this new thing,

    oh well, what can you do such is vba life

    This works a treat now -

    thank you and have a super day :)

    Cheers Dan :)


    • Edited by Dan_CS Wednesday, October 5, 2016 3:07 PM
    Wednesday, October 5, 2016 3:06 PM
  • You could also have tried replacing ^p<space>^p   with ^p
    Wednesday, October 5, 2016 3:24 PM
  • Oh  yes, that worked as well - who knew !

    I'll file this gem as well :)


    Cheers Dan :)

    Wednesday, October 5, 2016 3:52 PM
  • Before there were macros (back in the dark ages) cleaning up a document meant taking steps like

    Replace         with

    SpaceSpace     Space   (multiple times)

    Space^p        ^p

    ^p^p             ^p  (multiple times)

    and I forget what else.  But it was a lot of work....

    Wednesday, October 5, 2016 5:02 PM
  • What a relief we have evolved.

    I dont think us millenials know how good we have it  - spoilt rotten, our pain thresholds are under par, but then again VBA puts me in purgartory, stuck on a simple error for hours being hopeless with it!

    It's either that or copy paste all the way - a slow tortorous death  -  and no one has time for that

    :) :)


    Cheers Dan :)

    Wednesday, October 5, 2016 5:17 PM