![]() |
|
![]() |
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
![]()
How can I delete a blank line with Word VBA if it is followed by another blank line? So replace two empty lines with one empty line?
|
#2
|
||||
|
||||
![]()
The following should work - see Replace using wildcards
Code:
Sub Macro1() Dim oRng As Range Set oRng = ActiveDocument.Range With oRng.Find .ClearFormatting .Replacement.ClearFormatting .Text = "(^13){2,}" .Replacement.Text = "\1" .MatchWildcards = True .Execute Replace:=wdReplaceAll, Wrap:=wdFindStop End With Set oRng = Nothing 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
|
|||
|
|||
![]()
Thanks, unfortunately it does not work, or only if I use {2} instead of {2,}. However, it then unfortunately also deletes the blank line in cases like this (P for paragraph):
"blablablaP P blablabla" becomes "blablabla blablalba" Moreover, it does not delete the line here between two lines: last table row table 1 empty line empty line first table row table 2 |
#4
|
||||
|
||||
![]()
Can you post a sample document?
__________________
Graham Mayor - MS MVP (Word) (2002-2019) Visit my web site for more programming tips and ready made processes www.gmayor.com |
#5
|
||||
|
||||
![]()
Find/Replace can't be used to delete an empty paragraph before a table - even with a macro. For any other case, you don't even need a macro - simply use a wildcard Find/Replace, where:
Find = ^13{2,} Replace = ^p This is essentially what Graham's macro does.
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#6
|
|||
|
|||
![]()
Thank you very much for your help. Here is a sample document. Some tables in the document ever after are gone and I have a macro to remove blank row, but for example when a whole table is dropped there are no rows from that table but there are at least two blank rows (the blank row before and after the table).
Searching and replacing with macropod's search text somehow doesn't work for me, maybe it's the language settings, I'm going to try it with English. |
#7
|
||||
|
||||
![]()
You're evidently using a system with non-English regional settings. You could change the wildcard Find/Replace to:
Find = ^13{2;} Replace = ^p
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#8
|
|||
|
|||
![]()
Thanks, it unfortunately did not replace the two empty lines between two tables with an empty line.Did the replacement work for you with this document?
|
#9
|
||||
|
||||
![]()
As I said in post #5:
Quote:
Code:
Sub Demo() Application.ScreenUpdating = False With ActiveDocument.Range With .Find .ClearFormatting .Replacement.ClearFormatting .Text = "[^13]{2,}" .Replacement.Text = "^p" .Forward = True .Wrap = wdFindContinue .Format = False .MatchWildcards = True .Execute Replace:=wdReplaceAll .Wrap = wdFindStop End With Do While .Find.Execute .End = .End - 1 .Text = vbNullString .Collapse wdCollapseEnd Loop End With Application.ScreenUpdating = True End Sub
__________________
Cheers, Paul Edstein [Fmr MS MVP - Word] |
#10
|
|||
|
|||
![]()
Thanks for your help, the problem has been solved.
|
![]() |
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
![]() |
rgm60527 | Mail Merge | 2 | 02-22-2022 11:13 AM |
![]() |
Alex1s85 | Mail Merge | 4 | 01-18-2020 11:30 PM |
syntax for inserting blank line before inserting table and after a line or paragraph | SamDsouza | Word VBA | 8 | 08-04-2019 11:10 PM |
![]() |
Ziad El Hachem | Word VBA | 4 | 03-21-2017 06:55 PM |
![]() |
SteveWcg | Word | 5 | 01-08-2014 10:37 PM |