Removing trailing carriage returns in Word VBA?

S

Steve Marotta

Okay, so I've got a macro that's supposed to concatenate a bunch of
documents together, separated by exactly one blank line. The thing is,
I can't count on any specific number of blank lines at the end of each
source document that I'm concatenating. It would be nice if, after I
append each document to my master document, I could remove all trailing
carriage returns from the master document, so that I'll know exactly
where I am and how many blank lines I'm going to have after each
append.

I've scoured the books and newsgroups, and I haven't found a way to do
this. It seems simple enough, so maybe it's just my brain shorting out
after a long day. If anyone can shed some light on this, I would
vastly appreciate it.

~ Steve
 
H

Helmut Weber

Hi Steve,
if it is only about trailing empty (!) paragraphs, then

With ActiveDocument.Range.Paragraphs.Last
While .Range.Text = vbCr
.Range.Delete
Wend
End With

Often, however, such trailing paragraphs contain some whitespace.
In that case, we would have to extend the functionality quite a bit.

Greetings from Bavaria, Germany

Helmut Weber, MVP
"red.sys" & chr(64) & "t-online.de"
Word XP, Win 98
http://word.mvps.org/
 
S

Steve Marotta

Thanks! I actually had to tweak it a bit, but that's exactly what I
needed. For anybody who's interested in doing what I had to do, here's
the code I ended up using...

With ActiveDocument.Range.Characters
While .Last.Text = vbCr And .Last.Previous = vbCr
..Last.Delete
Wend
End With

The difference is that this code will delete *all* carriage returns at
the end of a document, leaving you with a document in which the ending
cursor position is the end of the last line of text. As pointed out,
though, if the trailing paragraphs contain whitespace, a bit more code
will be necessary.

Thanks again for the help.

~ Steve
 

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