Help with paragraph range

G

Greg Maxey

Hello,

As an example, I want to remove all text except the first character in each
paragraph.

I can use:

Sub Test()
Dim oPara As Paragraph
Dim oDoc As Document

Set oDoc = ActiveDocument
For Each oPara In oDoc.Paragraphs
With oPara.Range
.MoveEnd wdCharacter, -1
.MoveStart wdCharacter, 1
.Delete
End With
Next
End Sub

Now why is it that the following removes everything from the document? By
excluding the With statement is the oPara.Range reset to its orginal value
each time it is restated? Thanks.


Sub Test1()
Dim oPara As Paragraph
Dim rngPara As Range
Dim oDoc As Document
Dim i As Long

Set oDoc = ActiveDocument
For Each oPara In oDoc.Paragraphs
oPara.Range.MoveEnd wdCharacter, -1
oPara.Range.MoveStart wdCharacter, 1
oPara.Range.Delete
Next

End Sub
 
G

Greg Maxey

Jezebel,

In this case the For each is working fine in the first example I provided.
I am trying to understand why it works with:

With oPara.Range
.MoveEnd wdCharacter, -1
.MoveStart wdCharacter, 1
.Delete
End With

but not doesn't work in the second example.

oPara.Range.MoveEnd wdCharacter, -1
oPara.Range.MoveStart wdCharacter, 1
oPara.Range.Delete

I will have a look at your method. Since I don't monkey around with this
stuff on a regular basis I am trying to put together some notes on what
works and what doesn't.
 
J

Jonathan West

Hi Greg,


Greg Maxey said:
Jezebel,

In this case the For each is working fine in the first example I provided.
I am trying to understand why it works with:

With oPara.Range
.MoveEnd wdCharacter, -1
.MoveStart wdCharacter, 1
.Delete
End With

but not doesn't work in the second example.

oPara.Range.MoveEnd wdCharacter, -1
oPara.Range.MoveStart wdCharacter, 1
oPara.Range.Delete

In the second code sample you are trying to change the position of the range
property of a paragraph, in other words the oPara.Range property.

But the position of the start and end of the paragraph doesn't actually
change, so next time you drill down through oPara,Range, you get exactly the
same positions as before you tried moving.

Instead, you need to create another variable of type Range, and manipulate
that. This would do the business

Sub Test()
Dim oPara As Paragraph
Dim oDoc As Document
Dim oRange as Range

Set oDoc = ActiveDocument
For Each oPara In oDoc.Paragraphs
Set oRange = oPara.Range
oRange.MoveEnd wdCharacter, -1
oRange.MoveStart wdCharacter, 1
oRange.Delete
Next
End Sub

When you use the With oPara.Range-End With construction, VBA is actually
creating a hidden object variable of type Range which it then uses just for
the duration of the With block. In other words, the With block is doing much
the same job as oRange in my code sample. This is why your first code sample
works and your second one doesn't. However, it is not a behavior I would
feel comfortable relying on, so in cases like this I would recommend you
declare and use an object variable.
 
G

Greg Maxey

Jonathan,

Thank you for the explanation. I was thinking that something along that
line was going on but couldn't prove it.
 
J

Jeff

As a matter of interest, why do you use a document object?
Set oDoc = ActiveDocument
For Each oPara In oDoc.Paragraphs

why not use...

For Each oPara In ActiveDocument.Paragraphs

Jeff
 
G

Greg Maxey

Jeff,

In this case it was only because I was messing around with some snipets of
code and had cut that bit from a macro where I had used ActiveDocument
repeatedly.
 
J

Jeff

Is there an FAQ anywhere that discusses the advantages of declaring
and assigning objects rather than using the full object reference, or is it
simply a case of making the code neater?
 
J

Jean-Guy Marcil

Greg Maxey was telling us:
Greg Maxey nous racontait que :
Jeff,

I don't no of one and in view of recect research prompted by your
question perhaps it isn't such a good idea after all. See a
discussion here:
http://groups-beta.google.com/group..._doneTitle=Back+to+Search&&d#87f5a858d6edb92a


Hi Greg,
I thought, just in case you did not know about it, that I would let you know
about http://tinyurl.com.

Your link of 548 characters is reduced to:
http://tinyurl.com/4p88c

Cheers.
--

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

Jonathan West

Greg Maxey said:
Perhaps Jonathan West will be along to educate us both further on the
wisdom or fallacy of using Set oDoc = ActiveDocument

For a short macro that only takes a second or so to run, there is no great
advantage in using the extra line of code.

If you have Word 2000 or later, it is possible for the user to change the
active document by clicking on the document window in the taskbar. This can
happen even while a macro is running. This means that it is possible for the
ActiveDocument to change without warning during the course of a macro.

Therefore, if you have a macro that may run for a while, it is a good idea
to avoid using both the ActiveDocument and Selection objects. Replacing
ActiveDocument is quite straightforward. Sinply include the following at the
start of the macro

Dim oDoc as Document
Set oDoc = ActiveDocument

Then you use oDoc wherever you would normally use ActiveDocument.

Similarly, you can use a Range object for almost everything that the
Selection can do, like this

Dim oSelRange As Range
Set oSelRange = Selection.Range
 
J

Jeff

it is possible for the user to change the active document by clicking on
the document window in the taskbar. This can happen even while a macro is
running.

That's as good a reason as I need!
Jeff
 

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