Deleting unwanted lines

J

Joe

Hi. I'm new to vba and could use a little help.

I'm copying and pasting text from WordPerfect into Word.
Once it is in Word I use a macro to place the text in a
table and then use the table for mail merge labels.

When I copy and paste the name/address info from
WordPerfect sometimes it includes a blank line at the top
and a few blank lines after the text. This presents a
problem because my macro replaces paragraphs with tabs
before creating the table.

Question...How can I set this up so that if there are any
blank lines above the first line of text and any blank
lines below the last line of text they will be removed?

Thanks in advance for your help.
Joe
 
D

Doug Robbins - Word MVP - DELETE UPPERCASE CHARACT

Hi Joe,

If these blank lines area actually empty paragraphs ¶, so that actually you
have

Last line of address¶

First line of address¶

you can use Edit>Replace to replace ^p^p with ^p which would change the
above to

Last line of address¶
First line of address¶

Please post any further questions or followup to the newsgroups for the
benefit of others who may be interested. Unsolicited questions forwarded
directly to me will only be answered on a paid consulting basis.

Hope this helps
Doug Robbins - Word MVP
 
H

Helmut Weber

Hi Joe,
conversions from Wordperfect are a very special.
To get rid of all blank lines, you may have to
do a patternmatching search for "^011{1;}",
replace them with ^p. Word-Version greater than 97.
Search for resulting ^p^p (no pattern matching) and
replace it with ^p. And repeat that as long as ^p^p is
found. And to make sure beforehand, that there isn't more
than one ^p at the docs end. Otherwise you may end up
in an endless loop. To clear the docs end,
I use the following, which removes blanks before
paragraph marks, too.
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
"red.sys" & chr$(64) & "t-online.de"
 
J

Joe

Any suggestions on what to do if I have an empty paragraph appearing as the first line in the document.

blank line
First line of address

Is there a way to do a conditional statement where if the first line is blank delete it otherwise ignore?

Thanks for your help.
Joe
 
B

Bruce Brown

Joe

I don't know if this is what you want and it doesn't have the
advantage of removing blank spaces the way Helmut's code does.

What it does, though, is to eliminate *all* blank paragraphs in a
document except the last, regardless of how many consecutive blank
paragraphs it finds in a row, be it two or two hundred.

Dim R As Range, P As Paragraph
Set R = ActiveDocument.Range.Duplicate
R.End = R.End - 1
With R.Find
.Text = "^p^p"
.Replacement.Text = "^p"
Do While .Execute(Replace:=wdReplaceAll)
Loop
End With
If R.Paragraphs(1).Range.Characters.Count = 1 Then
R.Paragraphs(1).Range.Delete

- Bruce
 
J

JGM

Hi Bruce,

Can you please explain why you use the Duplicate property in this particular
case? I thought that this property, when used with a range object, was used
to keep the original range intact while expanding the "range" of the range
object, thus allowing us to refer to the original range later. In this case,
the range gets smaller, not bigger...

I tried your code with and without the Duplicate property, and to my eyes,
the results were the same. Since I know that you know what you are talking
about, I know I am missing something, and would like to know what it is!
(Phew! I know that you know what I want to know! lol... Seriously, I really
would like to know!)

Also, why won't the code get rid of the last paragraph if it is an "empty"
one, it then gets stuck in an infinite loop unless you add, as you did,
R.End = R.End - 1
and if you really want to delete that potentially "empty" last paragrpah,
you need something like:

If
ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count).Range.Characters.
Count = 1 Then
ActiveDocument.Paragraphs(ActiveDocument.Paragraphs.Count).Range.Delete
End If

???

TIA
 
H

Helmut Weber

.... and make sure that in "^011{1;}" the local version
of the list seperator is used. It may be ","
in most cases, but ";" in Germany.
....from Bavaria, Germany
Helmut Weber
"red.sys" & chr$(64) & "t-online.de"
 
B

Bruce Brown

Jean-Guy

I always use .Duplicate to base one range on another not because I
know what I'm doing but because, as you point out, it guarantees that
new ranges will be independent of old ones. It's like making a copy
of something you want to preserve, then making revisions on the copy.
(Not being clever enough to remember when .Duplicate *isn't* needed, I
use it indiscriminately. Never seems to hurt, though, as you saw for
yourself.)

You ask, "Also, why won't the code get rid of the last paragraph if it
is an "empty" one . . ?" Because the last paragraph mark can never be
deleted. The purpose of R.End = R.End - 1 was to exclude the last
character, always a blank paragraph mark, from the find/replace scope,
not to delete it.

You unfairly accuse me of knowing what I'm talking about, but I know
that you know that all I really know is whether stuff works or it
don't work based on the ancient Slobbovian art of testing, testing,
testing. - Bruce
 
J

JGM

You unfairly accuse me of knowing what I'm talking about, but I know
that you know that all I really know is whether stuff works or it
don't work based on the ancient Slobbovian art of testing, testing,
testing. - Bruce

lol
 

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