Stripping Hard Returns

J

Joan NYC

I have tried to do this with Pattern Matching to no avail

I am working on a job which entails reformatting some horribly formatted docs

I spent lots of time stripping out the hard returns manually

Everyone who worked on this mess didn't know about Paragraph formatting and
just kept pressing "return" which drives me crazy

Any suggestions would be appreciated

BTW, this is Word 2003 and I still cannot write code (I have tried!)
 
J

Jean-Guy Marcil

Joan NYC was telling us:
Joan NYC nous racontait que :
I have tried to do this with Pattern Matching to no avail

I am working on a job which entails reformatting some horribly
formatted docs

I spent lots of time stripping out the hard returns manually

Everyone who worked on this mess didn't know about Paragraph
formatting and just kept pressing "return" which drives me crazy

Any suggestions would be appreciated

BTW, this is Word 2003 and I still cannot write code (I have tried!)

No need for code, use Replace:

Make sure you check the Wildcard options (Click on the "More" button at the
bottom of the Replace dialog).

Then, in the "Find what:" field, type:
(^13)@
and in the "Replace with" field:
^13
and hit "Replace All"

This means: Find any multiple consecutive occurrences of ^13 (which is a
paragraph mark, or ¶) and replace that with a single one.
You will still need to manually delete the first and last ¶ if they are
present as the single character of the first and last paragraph of the
document.
Other than that it will be lightning quick!

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

jalanford

Hi Joan,

Here is a macro I use for doing just what you inquire about (written with
VBA 2003--though there is nothing in it that is specific to that release).
Since there is usually some ambiguity as to whether the return should be
deleted or replaced with a space, the macro queries this point (though it
doesn't offer the two options--which could easily be changed [for my
purposes, not offering the option has more to do with those running the macro
than the difficulty in coding]). It also uses "selection" which many on here
will caution against. Since the macro stops at every successful match, speed
is not really an issue. If you want to do away with the queries and let the
macro just strip and replace as it deems necessary, comment out the query
"if" statement. I would NOT recommend doing this. WARNING: this macro does
not find every errant return. You will still have to QC the doc. It does,
however, find most of them. You may want to consider something that looks for
alternate ways of breaking lines (soft return, page break, section break,
etc.) and converts them to regular hard returns (depending upon whether or
not you want to preserve the page and section breaks--I do not). The basic
method shown here could be expanded to include a lot more cases.


Sub CleanupExtraParas()

Selection.HomeKey Unit:=wdStory

Dim x As Integer
x = 0

Do

Selection.Find.ClearFormatting
With Selection.Find
.Text = ",^p"
.Replacement.Text = ", "
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
If Selection.Find.Found Then
If MsgBox("Should this hard return be replaced with a space?",
vbQuestion + vbYesNo) = vbYes Then
Selection.Delete
Selection.InsertBefore (", ")
End If
Else
x = 1
End If
Selection.MoveRight Unit:=wdCharacter, Count:=1
Loop Until x = 1

Selection.HomeKey Unit:=wdStory

x = 0

Do

Selection.Find.ClearFormatting
With Selection.Find
.Text = "([a-z])^13([a-z])"
.Replacement.Text = "\1 \2"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
If Selection.Find.Found Then
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
If MsgBox("Should this hard return be replaced with a space?",
vbQuestion + vbYesNo) = vbYes Then
Selection.Delete
Selection.InsertBefore (" ")
End If
Else
x = 1
End If
Selection.MoveRight Unit:=wdCharacter, Count:=1
Loop Until x = 1

Selection.HomeKey Unit:=wdStory

x = 0

Do

Selection.Find.ClearFormatting
With Selection.Find
.Text = "([-])^13([a-z])"
.Replacement.Text = "\1 \2"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
If Selection.Find.Found Then
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
If MsgBox("Should this hard return be deleted?", vbQuestion +
vbYesNo) = vbYes Then
Selection.Delete
'Selection.InsertBefore (" ")
End If
Else
x = 1
End If
Selection.MoveRight Unit:=wdCharacter, Count:=1
Loop Until x = 1

Selection.HomeKey Unit:=wdStory

x = 0

Do

Selection.Find.ClearFormatting
With Selection.Find
.Text = "([a-z] )^13([a-z])"
.Replacement.Text = "\1\2"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
If Selection.Find.Found Then
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
If MsgBox("Should this hard return be deleted?", vbQuestion +
vbYesNo) = vbYes Then
Selection.Delete
'Selection.InsertBefore (" ")
End If
Else
x = 1
End If
Selection.MoveRight Unit:=wdCharacter, Count:=1
Loop Until x = 1

End Sub

HTH,

jalanford
 
J

Joan NYC

Thanks Jean

I had tried something like that with the wildcards and this is good to know
for the future

HOWEVER, this is not a case of multiple returns

It is one per line

As if someone just sat on the return key for a few hours :)
 
J

Joan NYC

Thanks

I am going to try this on a test doc when I get home

Not sure if I want to try it here :)



jalanford said:
Hi Joan,

Here is a macro I use for doing just what you inquire about (written with
VBA 2003--though there is nothing in it that is specific to that release).
Since there is usually some ambiguity as to whether the return should be
deleted or replaced with a space, the macro queries this point (though it
doesn't offer the two options--which could easily be changed [for my
purposes, not offering the option has more to do with those running the macro
than the difficulty in coding]). It also uses "selection" which many on here
will caution against. Since the macro stops at every successful match, speed
is not really an issue. If you want to do away with the queries and let the
macro just strip and replace as it deems necessary, comment out the query
"if" statement. I would NOT recommend doing this. WARNING: this macro does
not find every errant return. You will still have to QC the doc. It does,
however, find most of them. You may want to consider something that looks for
alternate ways of breaking lines (soft return, page break, section break,
etc.) and converts them to regular hard returns (depending upon whether or
not you want to preserve the page and section breaks--I do not). The basic
method shown here could be expanded to include a lot more cases.


Sub CleanupExtraParas()

Selection.HomeKey Unit:=wdStory

Dim x As Integer
x = 0

Do

Selection.Find.ClearFormatting
With Selection.Find
.Text = ",^p"
.Replacement.Text = ", "
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = False
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
If Selection.Find.Found Then
If MsgBox("Should this hard return be replaced with a space?",
vbQuestion + vbYesNo) = vbYes Then
Selection.Delete
Selection.InsertBefore (", ")
End If
Else
x = 1
End If
Selection.MoveRight Unit:=wdCharacter, Count:=1
Loop Until x = 1

Selection.HomeKey Unit:=wdStory

x = 0

Do

Selection.Find.ClearFormatting
With Selection.Find
.Text = "([a-z])^13([a-z])"
.Replacement.Text = "\1 \2"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
If Selection.Find.Found Then
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
If MsgBox("Should this hard return be replaced with a space?",
vbQuestion + vbYesNo) = vbYes Then
Selection.Delete
Selection.InsertBefore (" ")
End If
Else
x = 1
End If
Selection.MoveRight Unit:=wdCharacter, Count:=1
Loop Until x = 1

Selection.HomeKey Unit:=wdStory

x = 0

Do

Selection.Find.ClearFormatting
With Selection.Find
.Text = "([-])^13([a-z])"
.Replacement.Text = "\1 \2"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
If Selection.Find.Found Then
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
If MsgBox("Should this hard return be deleted?", vbQuestion +
vbYesNo) = vbYes Then
Selection.Delete
'Selection.InsertBefore (" ")
End If
Else
x = 1
End If
Selection.MoveRight Unit:=wdCharacter, Count:=1
Loop Until x = 1

Selection.HomeKey Unit:=wdStory

x = 0

Do

Selection.Find.ClearFormatting
With Selection.Find
.Text = "([a-z] )^13([a-z])"
.Replacement.Text = "\1\2"
.Forward = True
.Wrap = wdFindStop
.Format = False
.MatchCase = False
.MatchWholeWord = False
.MatchWildcards = True
.MatchSoundsLike = False
.MatchAllWordForms = False
End With
Selection.Find.Execute
If Selection.Find.Found Then
Selection.MoveRight Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1
Selection.MoveLeft Unit:=wdCharacter, Count:=1, Extend:=wdExtend
If MsgBox("Should this hard return be deleted?", vbQuestion +
vbYesNo) = vbYes Then
Selection.Delete
'Selection.InsertBefore (" ")
End If
Else
x = 1
End If
Selection.MoveRight Unit:=wdCharacter, Count:=1
Loop Until x = 1

End Sub

HTH,

jalanford

Joan NYC said:
I have tried to do this with Pattern Matching to no avail

I am working on a job which entails reformatting some horribly formatted docs

I spent lots of time stripping out the hard returns manually

Everyone who worked on this mess didn't know about Paragraph formatting and
just kept pressing "return" which drives me crazy

Any suggestions would be appreciated

BTW, this is Word 2003 and I still cannot write code (I have tried!)
 
J

Joan NYC

Thanks Jay

I already saw Suzanne's article but it doesn't address the "exact" situation
I am encountering

I appreciate the help

I'll keep trying and/or manually deleting :)
 
J

Jean-Guy Marcil

Joan NYC was telling us:
Joan NYC nous racontait que :
Thanks Jean

I had tried something like that with the wildcards and this is good
to know for the future

HOWEVER, this is not a case of multiple returns

It is one per line

Please explain the difference between "multiple returns" and "one by line."

If I hit the Return key a "multiple" time, I get:






If I have one return by line, I have:






What am I missing?
As if someone just sat on the return key for a few hours :)

Have you tried my suggestion?
I think I had understood correctly the first time around.

Someone used the Return key to create spaces, so you get:

First Paragraph.¶





Second Paragraph.¶





Third Paragraph.¶





etc.

My suggestion will take care of this in a few milliseconds.

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
J

jalanford

Hi Jean-Guy,

I think she's talking about conceptual paragraphs, not Word paragraphs. The
conceptual paragraphs in the document she's working on have their line breaks
determined by returns instead of letting Word break the line. I attribute
this to authors treating word processors like they were typewriters.

As an example:

Line 1 para 1¶
Line 2 para 1¶
Line 3 para 1¶
¶
Line 1 para 2¶
Line 2 para 2¶
Line 3 para 2¶
Line 4 para 2¶
¶

Of course, Word sees this as 9 paragraphs. To the reader, it's only two.

Sorry for the intrusion.

Cheers,

jalanford
 
J

Joan NYC

What happened when I tried your suggestion that the whole doc became one big
paragraph!

I am going to experiment with it now that i am home

Thanks
 
J

Jean-Guy Marcil

jalanford was telling us:
jalanford nous racontait que :
Hi Jean-Guy,

I think she's talking about conceptual paragraphs, not Word
paragraphs. The conceptual paragraphs in the document she's working
on have their line breaks determined by returns instead of letting
Word break the line. I attribute this to authors treating word
processors like they were typewriters.

As an example:

Line 1 para 1¶
Line 2 para 1¶
Line 3 para 1¶

Line 1 para 2¶
Line 2 para 2¶
Line 3 para 2¶
Line 4 para 2¶


Of course, Word sees this as 9 paragraphs. To the reader, it's only
two.

Could be, I just wished that things were explained more clearly... so that
efficient help could be provided swiftly...
Hence my question, which has not been answered...

--
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
H

Helmut Weber

Hi Joan,

it all depends on the structure of the doc
to be processed. It is a trivial thing,
if (!) conceptual paragraphs are to be recognized
by two or more paragraph marks after each other.
If not, then there is no way.

Replace two ore more paragraph marks by a placeholder,
whereby a placeholder is a string otherwise not
to be found in the doc, like "÷÷".

Replace all chr(13) by a space.
Replace the placeholders by ^p.

Which leaves end-of-line-hyphenation problems.

HTH


--
Greetings from Bavaria, Germany

Helmut Weber, MVP WordVBA

Win XP, Office 2003
"red.sys" & Chr$(64) & "t-online.de"
 
J

Joan NYC

Thanks

This is a situation where the original "creator" of the doc didn't know how
to get to the next page. They figured they would just hit return until the
cursor wound up where they wanted

I did try lots of things before I enlisted aid

Guess it will have to be the old reliable delete key

It wouldn't be so bad but these docs that have to reformatted are between
700 and 900 pages. Talk about tedium. AND... there are section breaks
between every few pages due to the way they want the page numbering. On the
one I am working on now there are 250 section breaks so far and I am not even
3/4 finished!
 
R

Russ

Joan,
Amending what Jean-Guy said slightly should save time and preserve the
non-Word method of 'paragraphs'. But in the future know that Word expects a
paragraph to consist of continuous lines broken only by a Shift+Returns and
ended by a Return. The space between paragraphs is done by **formatting**
paragraphs with space before or after, not by using the Return key.
http://www.shaunakelly.com/word/concepts/introduction/index.html
--------------------------------------------
Make sure you check the Wildcard options (Click on the "More" button at the
bottom of the Replace dialog).

Then, in the "Find what:" field, type:
^13{3,} 'Use \n{3,} on a Mac
and in the "Replace with" field:
^p^p
and hit "Replace All"

This means: Find any 3 or more consecutive occurrences of ^13 (which is a
paragraph mark, or ¶) and replace that with two paragraph marks.
You will still need to manually delete the first and last ¶ if they are
present as the single character of the first and last paragraph of the
document.
Other than that it will be lightning quick!
---------------------------------------------
 
R

Russ

Joan,
I also want to remind you that when you are trying search patterns and they
don't quite work right, there is always the Undo function, as long as no
changes have been saved. Instead of using the Replace All option, you could
also use the other options to step through the document and apply changes
incrementally and Undo when the changes aren't satisfactory for certain
areas of the document.
 
J

Joan NYC

Hi Russ

I appreciate your reply but I do know all about formatting (former Word MVP,
btw)

The project is to do just that.... clean-up all the junk and reformat
correctly

Anything I do does not have one hard return in it

Just trying to figure out a shortcut to get rid of them easily which I
pretty much knew couldn't be done the conventional way which is why I posted
the question on the VBA board (which, I admit, I have had problems learning)

In fact I have printed out many areas of Shauna's website to give to my
coworkers as outline #ing is a big factor in this project and I found her
website the best for this

Thanks

Joan
 
J

Jonathan West

Joan NYC said:
I have tried to do this with Pattern Matching to no avail

I am working on a job which entails reformatting some horribly formatted
docs

I spent lots of time stripping out the hard returns manually

Everyone who worked on this mess didn't know about Paragraph formatting
and
just kept pressing "return" which drives me crazy

Any suggestions would be appreciated

BTW, this is Word 2003 and I still cannot write code (I have tried!)

Hi Joan

This macro should do nicely for you. Select some text you want multiple
returns strippeed out of, and run this macro

Sub StripMultipleReturns

Dim iEnd as Long

With Selection.Find
.Text = "^p^p"
.Replacement.Text = "^p"
.Format = False
.Wrap = wdFindStop
Do
iEnd = Selection.End
.Execute Replace:=wdReplaceAll
Loop While Selection.End < iEnd
End With

End Sub


What this does is continue to replace 2 paragraph marks with just one in the
selection until there are no more to replace. Because the replacement string
is shorter than the search string, it works this out by checking the end
position of the selection before and after each replace operation.
 
J

Joan NYC

I will try it and let you know

Thanks

Joan

Jonathan West said:
Hi Joan

This macro should do nicely for you. Select some text you want multiple
returns strippeed out of, and run this macro

Sub StripMultipleReturns

Dim iEnd as Long

With Selection.Find
.Text = "^p^p"
.Replacement.Text = "^p"
.Format = False
.Wrap = wdFindStop
Do
iEnd = Selection.End
.Execute Replace:=wdReplaceAll
Loop While Selection.End < iEnd
End With

End Sub


What this does is continue to replace 2 paragraph marks with just one in the
selection until there are no more to replace. Because the replacement string
is shorter than the search string, it works this out by checking the end
position of the selection before and after each replace operation.


--
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
 
J

Joan NYC

Thank you. Thank you. Thank you!!!

This is EXACTLY what I was looking for

Now I have to bring it to work tomorrow. It will save me tons of time and
wrist action!

I also want to thank everyone else for their input
 
R

Russ

Joan,
Here is what I suggested, also, to work with a **selection**, in case
Jonathan's routine doesn't satisfy your requirements. This routine is less
likely to make all the previous 'non-Word paragraphs' into one big 'non-Word
paragraph', if you select over multiple 'non-Word paragraphs'.
(Mac users need to substitute \n for ^13)

Sub StripMultipleReturns2()

With Selection.Find
.Text = "^13{3,}"
.Replacement.Text = "^p^p"
.MatchWildcards = True
.Format = False
.Wrap = wdFindStop
.Execute replace:=wdReplaceAll
End With

End Sub

After using above sub use this one to make the 'non-Word paragraphs' more
like Word paragraphs:
Sub PutSoftReturnsInParagraphs()

With Selection.Find
.Text = "[!^13]^13[!^13]"
.Replacement.Text = "^l"
.MatchWildcards = True
.Format = False
.Wrap = wdFindStop
.Execute replace:=wdReplaceAll
End With

End Sub

Then to reduce consecutive paragraph marks:
Sub EliminateContiguousParagraphMarks()

With Selection.Find
.Text = "^13{2,}"
.Replacement.Text = "^p"
.MatchWildcards = True
.Format = False
.Wrap = wdFindStop
.Execute replace:=wdReplaceAll
End With

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top