View Single Post
 
Old 03-27-2023, 12:20 AM
Dimsok Dimsok is offline Windows XP Office 2007
Advanced Beginner
 
Join Date: Sep 2014
Location: exUSSR
Posts: 50
Dimsok is on a distinguished road
Default Delete empty lines

I have found code, which delete empty paragraphs. But it doesn't do anything. Probably too old version doesn't find "^l"
Code:
Sub Deleemptylines()
Selection.Find.ClearFormatting
Selection.Find.Replacement.ClearFormatting
With Selection.Find
.Text = "^l"
.Replacement.Text = ""
.Forward = True
.Wrap = wdFindContinue
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchByte = False
.MatchAllWordForms = False
.MatchSoundsLike = False
.MatchWildcards = False
.MatchFuzzy = False
End With
Selection.Find.Execute Replace:=wdReplaceAll
End Sub
I try something like this, but for ^p^p, which replace on ^p. But it deletes only one emtpy par. at one time, so i should press more. So i found other code, which at last works:
Code:
Sub RemoveBlankParas()
    Dim oDoc        As Word.Document
    Dim i           As Long
    Dim oRng        As Range
    Dim lParas      As Long

    Set oDoc = ActiveDocument
    lParas = oDoc.Paragraphs.Count          ' Total paragraph count
    Set oRng = ActiveDocument.Range

    For i = lParas To 1 Step -1
        oRng.Select
        lEnd = lEnd + oRng.Paragraphs.Count                         ' Keep track of how many processed
        If Len(ActiveDocument.Paragraphs(i).Range.Text) = 1 Then
            ActiveDocument.Paragraphs(i).Range.Delete
        End If
    Next i

    Set para = Nothing
    Set oDoc = Nothing
    Exit Sub
End Sub
But i need another 2 variants of it:
1. If only one empty line, do nothing. If more then one, delete all except one.
2. The same as first (replace more then 1 emtpy lines on only 1 empty), but do it only if that empty lines are in the end of the document (no has any text after them).

For second variant i found:
Code:
Sub Delemtpylinatend()
Dim opar As Paragraph
Set opar = ActiveDocument.Range.Paragraphs.Last
While Len(opar.Range.Text) = 1
Set opar = opar.Previous
opar.Next.Range.Delete
Wend
End Sub

But it deletes all empty. How can i modify to leave one empty par?
Reply With Quote