PDA

View Full Version : [SOLVED:] Remove soft and/or hard returns



paulr1949
05-13-2020, 07:56 AM
Hi,

I'm pretty lousy at Word vba (although I can still write stuff in nexcel despite having been retuired 12 years).
Is it possible for anyone to tell me how I can use vba to go through a document and remove a return (either soft or hard) if there are two or more consecutively? I am archiving content from a forum which is about to close and the software is very good at putting extra returns in - and the document is currently at about 500 pages.

Thanks in advance
Paul

gmaxey
05-13-2020, 11:43 AM
Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oRng As Range
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = Chr(13) & "{2,}"
.Replacement.Text = Chr(13)
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Set oRng = ActiveDocument.Range
With oRng.Find
.Text = Chr(11) & "{2,}"
.Replacement.Text = Chr(11)
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
lbl_Exit:
Exit Sub
End Sub

paulr1949
05-13-2020, 02:24 PM
Many thanks Greg, that has done the trick. It has cleared about 50 pages (of c500)!

Paul

macropod
05-13-2020, 02:48 PM
Simpler and faster:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range.Find
.Text = "([^13^l])[^13^l]{1,}"
.Replacement.Text = "\1"
.MatchWildcards = True
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub

Paul_Hossler
05-13-2020, 06:56 PM
Greg --

What about Chr(13)Chr(11) and Chr(11)Chr(13) pairs?

I'd think you'd want them down to just a ^p

macropod
05-13-2020, 07:20 PM
The way the Find/Replace expressions are coded, it doesn't matter which way the [^13^l] (or [^13^11]) sequence is written. Moreover, whichever character in a Chr(13) & Chr(11) sequence occurs first, that is the character that gets used for the replacement. It's only where someone has foolishly used manual line breaks to add padding below a paragraph that things might come unstuck.

Paul_Hossler
05-13-2020, 07:48 PM
The way the Find/Replace expressions are coded, it doesn't matter which way the [^13^l] (or [^13^11]) sequence is written. Moreover, whichever character in a Chr(13) & Chr(11) sequence occurs first, that is the character that gets used for the replacement. It's only where someone has foolishly used manual line breaks to add padding below a paragraph that things might come unstuck.


If I'm reading Greg's macro correctly, it replaced 2 or more CR's with a single CR, and 2 or more LF's with a single LF

I was wondering about the case where there was a CR-LF pair or a LF-CR pair that the OP wanted turned into a single CR

macropod
05-13-2020, 07:55 PM
If I'm reading Greg's macro correctly, it replaced 2 or more CR's with a single CR, and 2 or more LF's with a single LF
His original macro, yes. What it didn't handle was a was a CR-LF pair or a LF-CR pair.


I was wondering about the case where there was a CR-LF pair or a LF-CR pair that the OP wanted turned into a single CR
My macro and Greg's second handle all combinations as described in post #8.

paulr1949
05-14-2020, 04:02 AM
Thank you both for your help. I do have instances of a soft return followed by a hard return and vv, but from a quick glance, most if not all of those seem to have been catered for and I'm happy sorting out the others - it's a lot better than going through 500 pages and deleting them one at a time :yes

macropod
05-14-2020, 05:34 AM
In that case:

Sub Demo()
Application.ScreenUpdating = False
With ActiveDocument.Range.Find
.MatchWildcards = True
.Text = "[^13]{2,}"
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
.Text = "[^l]{2,}"
.Replacement.Text = "^l"
.Execute Replace:=wdReplaceAll
.Text = "[^13^l]{2,}"
.Replacement.Text = "^p"
.Execute Replace:=wdReplaceAll
End With
Application.ScreenUpdating = True
End Sub