Deleting Lines in Word using VBA

M

Marcus

Is it possible using Word VBA to delete blank lines in a
word document. I cannot see how you use if then statements
and would be greatful for some advice.

The macro may need to run after a mail merge and could
consist of more than 20 pages will this pose a problem.

Many thanks
 
M

martinique

Easier just to use find and replace, run manually or run from within your
VBA code. Search for ^p^p and replace with ^p. Repeat until no substitutions
are made.
 
G

Graham Mayor

A wildcard search for
(^13){2,}
replace with
\1

will remove them all in one pass.

--
<>>< ><<> ><<> <>>< ><<> <>>< <>>< ><<>
Graham Mayor - Word MVP
E-mail (e-mail address removed)
Web site www.gmayor.dsl.pipex.com
Word MVP web site www.mvps.org/word
<>>< ><<> ><<> <>>< ><<> <>>< <>>< ><<>
 
P

Peter Jamieson

In addition to Graham's find/replace approach, if you are trying to remove
one-line gaps between tables, try the following VBA:

Dim p As Paragraph
For Each p In ActiveDocument.Paragraphs
If Len(p.Range.Text) = 1 Then
p.Range.Delete
End If
Next
End Sub

(Anyone know of a Find/Replace approach that will remove blank paragraphs
between tables?)

If you want to avoid deleting blank paragraphs /within/ tables, you can try:

Dim p As Paragraph
For Each p In ActiveDocument.Paragraphs
If Not p.Range.Information(wdWithInTable) Then
If Len(p.Range.Text) = 1 Then
p.Range.Delete
End If
End If
Next
End Sub

But beware! The above code is quite simplistic, It may be that p.Range.Text
is 1 even for paragraphs you do not want to delete (e.g. if a frame is
anchored to the paragraph)
 

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