Microsoft Office Forums

Go Back   Microsoft Office Forums > >

Reply
 
Thread Tools Display Modes
  #1  
Old 04-01-2022, 01:53 AM
jamu jamu is offline How can I delete a blank line with Word VBA if it is followed by another blank line? Windows 10 How can I delete a blank line with Word VBA if it is followed by another blank line? Office 2021
Novice
How can I delete a blank line with Word VBA if it is followed by another blank line?
 
Join Date: Mar 2022
Posts: 11
jamu is on a distinguished road
Default How can I delete a blank line with Word VBA if it is followed by another blank line?

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?
Reply With Quote
  #2  
Old 04-01-2022, 03:32 AM
gmayor's Avatar
gmayor gmayor is offline How can I delete a blank line with Word VBA if it is followed by another blank line? Windows 10 How can I delete a blank line with Word VBA if it is followed by another blank line? Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,138
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 ofgmayor has much to be proud of
Default

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
Reply With Quote
  #3  
Old 04-01-2022, 05:21 AM
jamu jamu is offline How can I delete a blank line with Word VBA if it is followed by another blank line? Windows 10 How can I delete a blank line with Word VBA if it is followed by another blank line? Office 2021
Novice
How can I delete a blank line with Word VBA if it is followed by another blank line?
 
Join Date: Mar 2022
Posts: 11
jamu is on a distinguished road
Default

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
Reply With Quote
  #4  
Old 04-01-2022, 06:50 AM
gmayor's Avatar
gmayor gmayor is offline How can I delete a blank line with Word VBA if it is followed by another blank line? Windows 10 How can I delete a blank line with Word VBA if it is followed by another blank line? Office 2019
Expert
 
Join Date: Aug 2014
Posts: 4,138
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 ofgmayor has much to be proud of
Default

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
Reply With Quote
  #5  
Old 04-01-2022, 04:52 PM
macropod's Avatar
macropod macropod is offline How can I delete a blank line with Word VBA if it is followed by another blank line? Windows 10 How can I delete a blank line with Word VBA if it is followed by another blank line? Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,374
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #6  
Old 04-09-2022, 04:03 PM
jamu jamu is offline How can I delete a blank line with Word VBA if it is followed by another blank line? Windows 10 How can I delete a blank line with Word VBA if it is followed by another blank line? Office 2021
Novice
How can I delete a blank line with Word VBA if it is followed by another blank line?
 
Join Date: Mar 2022
Posts: 11
jamu is on a distinguished road
Default

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.
Attached Files
File Type: docx example.docx (55.1 KB, 11 views)
Reply With Quote
  #7  
Old 04-10-2022, 06:44 AM
macropod's Avatar
macropod macropod is offline How can I delete a blank line with Word VBA if it is followed by another blank line? Windows 10 How can I delete a blank line with Word VBA if it is followed by another blank line? Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,374
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

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]
Reply With Quote
  #8  
Old 04-10-2022, 12:04 PM
jamu jamu is offline How can I delete a blank line with Word VBA if it is followed by another blank line? Windows 10 How can I delete a blank line with Word VBA if it is followed by another blank line? Office 2021
Novice
How can I delete a blank line with Word VBA if it is followed by another blank line?
 
Join Date: Mar 2022
Posts: 11
jamu is on a distinguished road
Default

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?
Reply With Quote
  #9  
Old 04-10-2022, 03:49 PM
macropod's Avatar
macropod macropod is offline How can I delete a blank line with Word VBA if it is followed by another blank line? Windows 10 How can I delete a blank line with Word VBA if it is followed by another blank line? Office 2016
Administrator
 
Join Date: Dec 2010
Location: Canberra, Australia
Posts: 22,374
macropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond reputemacropod has a reputation beyond repute
Default

As I said in post #5:
Quote:
Originally Posted by macropod View Post
Find/Replace can't be used to delete an empty paragraph before a table - even with a macro.
A macro you could use is:
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
The 'wdReplaceAll' reduces all consecutive paragraph breaks other than before a table to single paragraph breaks; the loop handles consecutive paragraph breaks before tables.
__________________
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Reply With Quote
  #10  
Old 04-11-2022, 04:21 PM
jamu jamu is offline How can I delete a blank line with Word VBA if it is followed by another blank line? Windows 10 How can I delete a blank line with Word VBA if it is followed by another blank line? Office 2021
Novice
How can I delete a blank line with Word VBA if it is followed by another blank line?
 
Join Date: Mar 2022
Posts: 11
jamu is on a distinguished road
Default

Thanks for your help, the problem has been solved.
Reply With Quote
Reply



Similar Threads
Thread Thread Starter Forum Replies Last Post
How can I delete a blank line with Word VBA if it is followed by another blank line? Making mail merge blank fill a line to highlight that line rgm60527 Mail Merge 2 02-22-2022 11:13 AM
How can I delete a blank line with Word VBA if it is followed by another blank line? Delete comma and space after blank merged value. Also remove Previous space and word before blank 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
How can I delete a blank line with Word VBA if it is followed by another blank line? Select and Delete 2 lines after each blank line Ziad El Hachem Word VBA 4 03-21-2017 06:55 PM
How can I delete a blank line with Word VBA if it is followed by another blank line? Deleting A blank Line that has a specific heading style , word 2010 & 2013 SteveWcg Word 5 01-08-2014 10:37 PM

Other Forums: Access Forums

All times are GMT -7. The time now is 09:12 AM.


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