none
Identify Section Breaks through VBA RRS feed

  • Question

  • Hi Everybody,

    I have written a macro to delete empty paragraphs from my document. It works fine but the only problem that I have is it also remove section breaks too. My code looks like this:

    Sub test()

    Dim para As Paragraph

    For Each para In ActiveDocument.Paragraphs

    If para.Range.Characters.Count <= 1 Then
    para.Range.Delete

    End If

    Next

    End Sub

    After a search through a web i found that there is a ASCII code for section break as Chr(12) but so i tried to check the range contain this character. But it didn't seemed to work. Any Suggestions??


    Regards, __________________________________ Supun Samarakoon

    Wednesday, October 19, 2016 5:44 PM

Answers

  • You could use

    Sub test()
        Dim para As Paragraph
        For Each para In ActiveDocument.Paragraphs
        If para.Range.Characters.Count = 1 Then
            If para.Range.Characters(1) <> vbFormFeed Then
                para.Range.Delete
            End If
        End If
        Next para
    End Sub

    vbFormFeed is Chr(12).


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Proposed as answer by Chenchen LiModerator Thursday, October 20, 2016 2:00 AM
    • Marked as answer by Supunsam Thursday, October 20, 2016 4:22 PM
    Wednesday, October 19, 2016 8:45 PM
  • You could do this with a wildcard Find/Replace, without the need for a macro and without the risk of deleting Section breaks. Simply use:

    Find = [^13]{2,}
    Replace= ^p

    Of course, the above could also be implemented as a macro - and would be far faster than what you're now using:

    Sub Demo()
    Application.ScreenUpdating = True
    With ActiveDocument.Range.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "[^13]{2,}"
      .Replacement.Text = "^p"
      .Forward = True
      .Wrap = wdFindContinue
      .Format = False
      .MatchWildcards = True
      .Execute Replace:=wdReplaceAll
    End With
    Application.ScreenUpdating = False
    End Sub

    The only empty paragraphs the above will miss are those immediately before a table; that is apparently by design, so that tables separated by empty paragraphs don't get joined.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Proposed as answer by Chenchen LiModerator Thursday, October 20, 2016 2:00 AM
    • Marked as answer by Supunsam Thursday, October 20, 2016 4:22 PM
    Wednesday, October 19, 2016 10:20 PM

All replies

  • You could use

    Sub test()
        Dim para As Paragraph
        For Each para In ActiveDocument.Paragraphs
        If para.Range.Characters.Count = 1 Then
            If para.Range.Characters(1) <> vbFormFeed Then
                para.Range.Delete
            End If
        End If
        Next para
    End Sub

    vbFormFeed is Chr(12).


    Regards, Hans Vogelaar (http://www.eileenslounge.com)

    • Proposed as answer by Chenchen LiModerator Thursday, October 20, 2016 2:00 AM
    • Marked as answer by Supunsam Thursday, October 20, 2016 4:22 PM
    Wednesday, October 19, 2016 8:45 PM
  • You could do this with a wildcard Find/Replace, without the need for a macro and without the risk of deleting Section breaks. Simply use:

    Find = [^13]{2,}
    Replace= ^p

    Of course, the above could also be implemented as a macro - and would be far faster than what you're now using:

    Sub Demo()
    Application.ScreenUpdating = True
    With ActiveDocument.Range.Find
      .ClearFormatting
      .Replacement.ClearFormatting
      .Text = "[^13]{2,}"
      .Replacement.Text = "^p"
      .Forward = True
      .Wrap = wdFindContinue
      .Format = False
      .MatchWildcards = True
      .Execute Replace:=wdReplaceAll
    End With
    Application.ScreenUpdating = False
    End Sub

    The only empty paragraphs the above will miss are those immediately before a table; that is apparently by design, so that tables separated by empty paragraphs don't get joined.


    Cheers
    Paul Edstein
    [MS MVP - Word]

    • Proposed as answer by Chenchen LiModerator Thursday, October 20, 2016 2:00 AM
    • Marked as answer by Supunsam Thursday, October 20, 2016 4:22 PM
    Wednesday, October 19, 2016 10:20 PM
  • Thanks you very much Hans and Paul. 

    Both methods worked for me. Thank you very much for your support and guidance.



    Regards,

    Supun Samarakoon



    • Edited by Supunsam Thursday, October 20, 2016 4:25 PM
    Thursday, October 20, 2016 4:25 PM
  • Simple solution! I see 2 (more) problems (I'm not sure whether I like the empty-paragraph-before-table behavior), however:

    a) It also misses an empty paragraph at the very end of the document.

    b) It apparently depends on the locale. I had to change '.Text = "[^13]{2,}"' to '.Text = "[^13]{2;}"' (note the semicolon)

    Wednesday, October 2, 2019 6:50 AM