delete empty paragraphs

F

filo666

I'm runing a macro that eliminates all the paragraphs that start with ++, the
code brakes down when I have paragraphs with no information (any letters or
characters, like if the enter key was pressed), I though about

if ativedocument.paragraphs(n).range="" then
paragraphs(n).delete
end if

and the for structure could continue

but it didn't work out

any suggestions??

TIA
 
J

Jean-Guy Marcil

filo666 was telling us:
filo666 nous racontait que :
I'm runing a macro that eliminates all the paragraphs that start with
++, the code brakes down when I have paragraphs with no information
(any letters or characters, like if the enter key was pressed), I
though about

if ativedocument.paragraphs(n).range="" then
paragraphs(n).delete
end if

and the for structure could continue

but it didn't work out

any suggestions??

TIA

Replace
if ativedocument.paragraphs(n).range="" then (sic)
with
If ActiveDocument.Paragraphs(n).Range.Text = Chr(13) Then
The range of the paragraph always includes the ¶ at the end of the
paragraph, so it is never totally empty.
Also, I believe that it is better to explicitly declare a property instead
of relying on the default property ".Text" in this case. It makes the code
more explicit and if the VBA compiler should changes its behaviour regarding
default properties in a future Word version, your code will still run
correctly.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
S

Stefan Blom

The problem here is that "an empty paragraph" isn't really empty: it has
a paragraph mark. Instead, you'll have to test for an empty string on a
range object which is one character "shorter." Something like this
should work:

Dim n As Integer ' loop variable
Dim r As Range ' range object variable

For n = ActiveDocument.Paragraphs.Count To 1 Step -1
Set r = ActiveDocument.Paragraphs(n).Range.Duplicate
r.End = r.End - 1

If r.Text = "" Then

ActiveDocument.Paragraphs(n).Range.Delete

End If

Next n

Note that I'm stepping backwards, because otherwise the value of the
Count property used in the loop would be incorrect as soon as the first
paragraph has been deleted.

Alternatively, use Find and Replace to get rid of the empty paragraphs
*before* running the code you already have.

--
Stefan Blom
Microsoft Word MVP


in message
news:[email protected]...
 
J

Jean-Guy Marcil

Stefan Blom was telling us:
Stefan Blom nous racontait que :
The problem here is that "an empty paragraph" isn't really empty: it
has a paragraph mark. Instead, you'll have to test for an empty
string on a range object which is one character "shorter." Something
like this should work:

Dim n As Integer ' loop variable
Dim r As Range ' range object variable

For n = ActiveDocument.Paragraphs.Count To 1 Step -1
Set r = ActiveDocument.Paragraphs(n).Range.Duplicate
r.End = r.End - 1

If r.Text = "" Then

ActiveDocument.Paragraphs(n).Range.Delete

End If

Next n

Note that I'm stepping backwards, because otherwise the value of the
Count property used in the loop would be incorrect as soon as the
first paragraph has been deleted.

Hi Stefan,

Any reason why you use r.End... etc?

Wouldn't it be simpler to use:

'_______________________________________
Dim n As Long
Dim r As Range

For n = ActiveDocument.Paragraphs.Count To 1 Step -1
Set r = ActiveDocument.Paragraphs(n).Range
If r.Text = Chr(13) Then
ActiveDocument.Paragraphs(n).Range.Delete
End If
Next
'_______________________________________

???

By the way, I used
Dim n As Long
because the Integer type is not used anymore, they are all converted to Long
by the compiler anyway.

--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
S

Stefan Blom

Hi Jean-Guy

in message
Stefan Blom was telling us:
Stefan Blom nous racontait que :


Hi Stefan,

Any reason why you use r.End... etc?

This is the space where the VBA expert presents his convincing
arguments, I guess. :) For me, I just used the first solution I could
think of (well aware that there could be others).
Wouldn't it be simpler to use:

'_______________________________________
Dim n As Long
Dim r As Range

For n = ActiveDocument.Paragraphs.Count To 1 Step -1
Set r = ActiveDocument.Paragraphs(n).Range
If r.Text = Chr(13) Then
ActiveDocument.Paragraphs(n).Range.Delete
End If
Next
'_______________________________________

???

Good point!

By the way, I used
Dim n As Long
because the Integer type is not used anymore, they are all converted to Long
by the compiler anyway.

Is this new with Word 2007?
 
J

Jean-Guy Marcil

Stefan Blom was telling us:
Stefan Blom nous racontait que :
Hi Jean-Guy

in message


This is the space where the VBA expert presents his convincing
arguments, I guess. :) For me, I just used the first solution I could
think of (well aware that there could be others).

As long as it works!
I was just curious in case I had missed something...
Good point!



Is this new with Word 2007?

No.
I believe it has to do with the fact that 32 bit computers cannot allocate a
memory space as small as an Integer, due to the way the memory is allocated,
the smallest unit is equivalent to a Long. So if you declare an Integer, it
has to be converted to a Long to fit in the allocated memory space.
But I may have misunderstood this point as I am not a computer architecture
expert...


--

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
(e-mail address removed)
Word MVP site: http://www.word.mvps.org
 
S

Stefan Blom

I believe it has to do with the fact that 32 bit computers cannot
allocate a
memory space as small as an Integer, due to the way the memory is allocated,
the smallest unit is equivalent to a Long. So if you declare an Integer, it
has to be converted to a Long to fit in the allocated memory space.
But I may have misunderstood this point as I am not a computer architecture
expert...

Thank you for clarifying this.

--
Stefan Blom
Microsoft Word MVP


in message
 

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