1

While we type in word a new line can be created by Shift + Enter which is called a line break. Recently, I copied some text from a web site into a word and found that there is large number of unwanted line breaks in the document. Removing each of them manually would be very time consuming. So I set about how to do it. I could not find an answer even after searching for the same on the net. Then finally I got an answer from the comments section of this page. https://www.oakleys.org.uk/blog/2017/05/how_to_insert_new_line_line_break_in_microsoft_word_using_vba.

My purpose was to remove the line breaks and how I did that is given in the VBA program which I am posting in the answer your own question for those who will be in similar situations and desires to achieve it through VBA.

2 Answers2

1

You can achieve the same with Find & replace too:

  • press CTRL+H to open replace dialog
  • type ^l^l to "find what"
  • type ^l to "replace to"
  • make sure "Use wildcards" is unchecked
  • press replace all
0
Sub HowtoRemoveLineBreaksFromDocx

    Dim achar As Characters
    Set achar = ActiveDocument.Characters

    Dim i As Integer
    i = 0
    For Each oChar In achar

        If (oChar = vbVerticalTab) Then

            i = i + 1 

            If (i = 2) Then
                oChar.Delete
                i = 0
            End If

       End If

    Next oChar

End Sub