Deleting empty paragraphs

K

Kas

I need to write code that will delete all of the empty
paragraphs in a document. Any help on this would be
greatly appreciated.

Thanks!
 
H

Helmut Weber

Hi Kas,
how about that:
Dim op As Paragraph
For Each op In ActiveDocument.Paragraphs
If Len(op.Range.Text) = 1 Then op.Range.Delete
Next
Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr$(64) & "t-online.de"
Word 97, NT4.0
 
J

Jay Freedman

Hi, Helmut,

That would work, but this is a *lot* quicker, especially in a big document:

Dim oRg As Range
Set oRg = ActiveDocument.Range
With oRg.Find
.ClearFormatting
.Text = "^13{2,}"
.Replacement.Text = "^p"
.MatchWildcards = True
.Format = False
.Forward = True
.Wrap = wdFindContinue
.Execute Replace:=wdReplaceAll
End With
 
H

Helmut Weber

Hi Jay,
....but besides being faster, your code leaves this
tricky empty paragraph at the end of a document
untouched. And note, as you have probably read,
.Text = "^13{2,}" wouldn't work in a german version
and maybe in some other localized versions, either,
";" has to be used instead:
..Text = "^13{2;}"
Greetings from Bavaria, Germany
Helmut Weber
"red.sys" & chr$(64) & "t-online.de"
Word XP, NT 4.0
 
H

Harold Kless[MSFT}

Try this:
Sub RemoveBlanks()
Dim myParagraph As Paragraph

For Each myParagraph In ActiveDocument.Paragraphs
If Len(myParagraph.Range.Text) <= 1 Then
myParagraph.Range.Delete
End If

Next

End Sub

Harold Kless, MCSD
Support Professional
Microsoft Technical Support for Business Applications
(e-mail address removed)

--


This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 

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