Macro hangs up on last para return

S

Steven Drenker

I want to remove all instances of two or more paragraph returns and convert
them to single paragraph return. My code goes into infinite loop on the last
line of a document when it has two or more returns.

I know this has been addressed many times. How do I solve this?
TIA,

Steve
 
J

Jay Freedman

Hi Steven

It's a common mistake to try to program a loop to do jobs like this.
Instead, you should use the Find/Replace feature and do a Replace All.
You can do it manually or with a macro.

Here are the manual steps:
- In the Replace dialog, click the More button and check the "Use
wildcards" box.
- In the Find What box, type
[^13]{2,}
- In the Replace With box, type
^p
- Click the Replace All button.

You can record this with the macro recorder to make a macro you can
save in a template.

See http://word.mvps.org/FAQs/General/UsingWildcards.htm for more
about wildcard searches, and also
http://word.mvps.org/FAQs/Formatting/CleanWebText.htm for more about
cleaning up badly formatted documents.
 
H

Helmut Weber

Hi Steven,
have a look at this,
which sould be run before replacing 2 or more immediately
following paragraph marks by one paragraph mark:
Public Sub PurgeDocEnd()
Dim z As Long
Dim r As Range
With ActiveDocument
z = .Range.End
Set r = .Range(z - 2, z - 1)
While r.Text = " " Or r.Text = Chr$(13)
r.Delete
z = .Range.End
Set r = .Range(z - 2, z - 1)
Wend
End With
End Sub
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
 
S

Steven Drenker

Helmut...thanks, I'll give that a try.

Jay...thanks for your help. I perhaps wasn't clear on what I wanted. I have
a large macro that cleans up docs I grab from the web (technology & market
research) and formats them in a consistent way. The problem with the double
para returns at the end of the doc is only one of many formatting problems I
am addressing. Hence, the need for automation.

Steve
 
S

Steven Drenker

Helmut...I had been using similar code as follows:

Set MyRange = ActiveDocument.Paragraphs.Last.Range
Do While MyRange.Text = vbCr
MyRange.Delete
Set MyRange = ActiveDocument.Paragraphs.Last.Range
Loop

But occasionally it still hangs at the end (I think that's where the
infinite loop is).

Thoughts? Maybe the problem is my failure to strip trailing spaces like you
do?

Steve
 
H

Helmut Weber

Hi Steve,
Thoughts?
many thoughts. But to no clear result. Sometimes one has to
be satisfied with the best known solution, no matter whether one
understands all details. I got about 50 users and am managing more
than 250,000 docs, and PurgeDocEnd has been working for years.
Maybe I got to rethink it tomorrow.
I can see, that your approach fails, e.g., in case there are empty
paragraphs at the doc's end, which are preceeded by a table.
And, you are attempting to delete the last paragraph, which
may be possible in a way, but at the very same moment, the
immediately preceeding paragraph becomes the last paragraph.
Forgive me, for being talkative. From your second name, I assume,
that you know what is is, to cut off a slice from the end of a salami.
You can't do it without generating a new end at once.
I think, deleting the last paragraph is a fake. It is really,
that the last paragraph is changing position, if it can ...
If it cann't you get an endless loop.
Greetings from Bavaria, Germany
Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
 

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