none
Removing blank line RRS feed

  • Question

  • Hi

    I am replacing text in a line with null string making the line blank. How can I remove the blank line altogether? Can I for instance replace paragraph as part of line text replacement?

    Thanks

    Regards

    Saturday, February 18, 2017 6:03 AM

Answers

  • Hi Y a h y a,

    you can find for "^p^p" and replace it with "^p" to remove empty lines.

    code:

    Sub demo()
     
        With ActiveDocument.Range.Find
          .Text = "^p^p"
          .Replacement.Text = "^p"
          .Replacement.ClearFormatting
          .Replacement.Font.Italic = False
          .Forward = True
          .Wrap = wdFindContinue
          .Format = False
          .MatchCase = False
          .MatchWholeWord = False
          .MatchWildcards = False
          .MatchSoundsLike = False
          .MatchAllWordForms = False
          .Execute Replace:=wdReplaceAll
        End With
    End Sub

    document before running the code:

    Output:

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 20, 2017 12:44 AM
    Moderator

All replies

  • You have given no meaningful detail on how the replacement is being done. Most assuredly, however, paragraph and line breaks breaks can be deleted, including via Find/Replace (for which the Find expression for paragraph breaks is either ^p or ^13 and the Find expression for paragraph breaks is either ^l or ^11).

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Sunday, February 19, 2017 9:29 PM
  • Hi Y a h y a,

    you can find for "^p^p" and replace it with "^p" to remove empty lines.

    code:

    Sub demo()
     
        With ActiveDocument.Range.Find
          .Text = "^p^p"
          .Replacement.Text = "^p"
          .Replacement.ClearFormatting
          .Replacement.Font.Italic = False
          .Forward = True
          .Wrap = wdFindContinue
          .Format = False
          .MatchCase = False
          .MatchWholeWord = False
          .MatchWildcards = False
          .MatchSoundsLike = False
          .MatchAllWordForms = False
          .Execute Replace:=wdReplaceAll
        End With
    End Sub

    document before running the code:

    Output:

    Regards

    Deepak


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Monday, February 20, 2017 12:44 AM
    Moderator
  • You have given no meaningful detail on how the replacement is being done. Most assuredly, however, paragraph and line breaks breaks can be deleted, including via Find/Replace (for which the Find expression for paragraph breaks is either ^p or ^13 and the Find expression for paragraph breaks is either ^l or ^11).

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Hi

    So I have some text on a line 'Some Text'. I replace it with null string so the line ends up with no text. However the line is blank and I need to get rid of it.

    Regards

    Monday, February 20, 2017 6:31 AM
  • Yes, we already knew that, but what you haven't given is any context. It's also not apparent why you're replacing the text without also replacing the character that follows it as part of the same process. That simple step would eliminate the paragraph/line break and obviate the need for additional code.

    Cheers
    Paul Edstein
    [MS MVP - Word]

    Monday, February 20, 2017 7:43 AM