#1
|
|||
|
|||
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 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 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? |
#2
|
||||
|
||||
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 |
#3
|
|||
|
|||
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.
|
#4
|
|||
|
|||
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? |
#5
|
|||
|
|||
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.
|
#6
|
|||
|
|||
Quote:
“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 |
#7
|
|||
|
|||
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).
|
|
Similar Threads | ||||
Thread | Thread Starter | Forum | Replies | Last Post |
A way to delete when 2nd column is empty | gavi12 | Word VBA | 1 | 04-04-2020 05:42 AM |
Delete row with empty cel in a table | vibor | Word VBA | 9 | 05-03-2015 05:42 AM |
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 |