Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 03-27-2023, 12:20 AM
Dimsok Dimsok is offline Delete empty lines Windows XP Delete empty lines Office 2007
Advanced Beginner
Delete empty lines
 
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
  #2  
Old 03-27-2023, 01:30 AM
gmayor's Avatar
gmayor gmayor is offline Delete empty lines Windows 10 Delete empty lines Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,101
gmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud ofgmayor has much to be proud of
Default

The following would leave one empty paragraph between paragraphs, but it would be far better to remove all empty paragraphs and apply styles to give the spacing you require. See also Replace using wildcards

Code:
Sub Macro1()
    With Selection.Find
        .ClearFormatting
        .Replacement.ClearFormatting
        .Text = "^13{2,}"
        .Replacement.Text = "^p^p"
        .Forward = True
        .Wrap = wdFindContinue
        .Format = False
        .MatchCase = False
        .MatchWholeWord = False
        .MatchAllWordForms = False
        .MatchSoundsLike = False
        .MatchWildcards = True
        .Execute Replace:=wdReplaceAll
    End With
End Sub
__________________
Graham Mayor - MS MVP (Word) (2002-2019)
Visit my web site for more programming tips and ready made processes www.gmayor.com
Reply With Quote
  #3  
Old 03-28-2023, 11:09 PM
Dimsok Dimsok is offline Delete empty lines Windows XP Delete empty lines Office 2007
Advanced Beginner
Delete empty lines
 
Join Date: Sep 2014
Location: exUSSR
Posts: 50
Dimsok is on a distinguished road
Default

Doesn't work for me. Probably is too old ms word. I try in find dialog using ^13{2,} with wildcard but it doesn't find anything.
Reply With Quote
  #4  
Old 04-11-2023, 11:31 AM
ctviggen ctviggen is offline Delete empty lines Windows 10 Delete empty lines Office 2016
Advanced Beginner
 
Join Date: Feb 2021
Posts: 54
ctviggen is on a distinguished road
Default

Not directly related, but why can you also put the execute replace statement outside the with...end with "loop"?



In other words, this:


Code:
Sub Macro1()     With Selection.Find         .ClearFormatting         .Replacement.ClearFormatting         .Text = "^13{2,}"         .Replacement.Text = "^p^p"         .Forward = True         .Wrap = wdFindContinue         .Format = False         .MatchCase = False         .MatchWholeWord = False         .MatchAllWordForms = False         .MatchSoundsLike = False         .MatchWildcards = True         ' .Execute Replace:=wdReplaceAll     End With
    Selection.Find.Execute Replace:=wdReplaceAll End Sub

Does the Selection.Find just find everything...but do nothing until the execute? If so, why don't you HAVE to put the replace all outside the loop?
Reply With Quote
  #5  
Old 04-11-2023, 11:33 AM
ctviggen ctviggen is offline Delete empty lines Windows 10 Delete empty lines Office 2016
Advanced Beginner
 
Join Date: Feb 2021
Posts: 54
ctviggen is on a distinguished road
Default

Hmm...not sure why that looks bad. Anyway, the Replace:=wdReplaceAll is outside the original with..end with "loop", instead of being inside the loop.
Reply With Quote
  #6  
Old 04-11-2023, 12:24 PM
Italophile Italophile is online now Delete empty lines Windows 11 Delete empty lines Office 2021
Expert
 
Join Date: Mar 2022
Posts: 333
Italophile is just really niceItalophile is just really niceItalophile is just really niceItalophile is just really nice
Default

Quote:
Originally Posted by ctviggen View Post
Hmm...not sure why that looks bad. Anyway, the Replace:=wdReplaceAll is outside the original with..end with "loop", instead of being inside the loop.
Firstly, there isn’t a “Loop” in your code, nor is there an “End With”

“With … End With” is referred to as a “With Block”, and is an efficient way of referring to an object without having to repeat the object each time you set a property.

“Selection.Find” does not do anything until the “.Execute”, which may be within the With block or outside it, especially if it is being called inside a loop, in which case it would appear as
Code:
Do While Selection.Find.Execute
    ‘Do stuff
Loop
Reply With Quote
  #7  
Old 04-16-2023, 11:38 AM
ctviggen ctviggen is offline Delete empty lines Windows 10 Delete empty lines Office 2016
Advanced Beginner
 
Join Date: Feb 2021
Posts: 54
ctviggen is on a distinguished road
Default

Thanks. I also realize that the .Execute can be run twice in a row. I've done this to replace two spaces with one space (and running it twice in a row replaces three spaces with one).
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete empty lines A way to delete when 2nd column is empty gavi12 Word VBA 1 04-04-2020 05:42 AM
Delete empty lines Delete row with empty cel in a table vibor Word VBA 9 05-03-2015 05:42 AM
Delete empty lines Remove empty lines at the top of every page dexter30 Word VBA 2 08-05-2013 08:37 PM
regular expressions for empty lines eNGiNe Word 1 01-21-2013 06:38 AM
Deleting empty lines lostsoul62 Word 5 04-16-2012 04:55 AM

Other Forums: Access Forums

All times are GMT -7. The time now is 05:56 AM.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Search Engine Optimisation provided by DragonByte SEO (Lite) - vBulletin Mods & Addons Copyright © 2024 DragonByte Technologies Ltd.
MSOfficeForums.com is not affiliated with Microsoft