
Signature
Greg Maxey/Word MVP
A Peer in Peer to Peer Support
Hi Greg,
> Jezebel,
>
[quoted text clipped - 12 lines]
> 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.

Signature
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Greg Maxey - 17 Jan 2005 13:49 GMT
Jonathan,
Thank you for the explanation. I was thinking that something along that
line was going on but couldn't prove it.

Signature
Greg Maxey/Word MVP
A Peer in Peer to Peer Support
> Hi Greg,
>
[quoted text clipped - 47 lines]
> relying on, so in cases like this I would recommend you declare and
> use an object variable.
Jeff - 17 Jan 2005 22:00 GMT
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
> Hi Greg,
>
[quoted text clipped - 46 lines]
> behavior I would feel comfortable relying on, so in cases like this I
> would recommend you declare and use an object variable.
Greg Maxey - 17 Jan 2005 22:39 GMT
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.

Signature
Greg Maxey/Word MVP
A Peer in Peer to Peer Support
> As a matter of interest, why do you use a document object?
>
[quoted text clipped - 65 lines]
>> www.intelligentdocuments.co.uk
>> Please reply to the newsgroup
Jeff - 17 Jan 2005 22:56 GMT
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?
> Jeff,
>
[quoted text clipped - 71 lines]
> >> www.intelligentdocuments.co.uk
> >> Please reply to the newsgroup
Greg Maxey - 17 Jan 2005 23:36 GMT
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/microsoft.public.word.vba.general/browse_thr
ead/thread/31c53f9191b3f4bf/87f5a858d6edb92a?q=Why+use+oDoc+group:Microsoft.Publ
ic.Word.*&_done=%2Fgroups%3Fas_q%3DWhy+use+oDoc%26num%3D10%26scoring%3Dr%26hl%3D
en%26ie%3DUTF-8%26as_epq%3D%26as_oq%3D%26as_eq%3D%26as_ugroup%3DMicrosoft.Public
.Word.*%26as_usubject%3D%26as_uauthors%3D%26lr%3D%26as_drrb%3Dq%26as_qdr%3D%26as
_mind%3D1%26as_minm%3D1%26as_miny%3D1981%26as_maxd%3D17%26as_maxm%3D1%26as_maxy%
3D2005%26safe%3Doff%26&_doneTitle=Back+to+Search&&d#87f5a858d6edb92a
Perhaps Jonathan West will be along to educate us both further on the wisdom
or fallacy of using Set oDoc = ActiveDocument

Signature
Greg Maxey/Word MVP
A Peer in Peer to Peer Support
> Is there an FAQ anywhere that discusses the advantages of declaring
> and assigning objects rather than using the full object reference, or
[quoted text clipped - 79 lines]
>>>> www.intelligentdocuments.co.uk
>>>> Please reply to the newsgroup
Jean-Guy Marcil - 18 Jan 2005 03:16 GMT
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/microsoft.public.word.vba.general/browse_thr
ead/thread/31c53f9191b3f4bf/87f5a858d6edb92a?q=Why+use+oDoc+group:Microsoft.Publ
ic.Word.*&_done=%2Fgroups%3Fas_q%3DWhy+use+oDoc%26num%3D10%26scoring%3Dr%26hl%3D
en%26ie%3DUTF-8%26as_epq%3D%26as_oq%3D%26as_eq%3D%26as_ugroup%3DMicrosoft.Public
.Word.*%26as_usubject%3D%26as_uauthors%3D%26lr%3D%26as_drrb%3Dq%26as_qdr%3D%26as
_mind%3D1%26as_minm%3D1%26as_miny%3D1981%26as_maxd%3D17%26as_maxm%3D1%26as_maxy%
3D2005%26safe%3Doff%26&_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.

Signature
Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org
Greg Maxey - 18 Jan 2005 03:36 GMT
JGM,
Thanks. I will have a look at it.

Signature
Greg Maxey/Word MVP
A Peer in Peer to Peer Support
> Greg Maxey was telling us:
> Greg Maxey nous racontait que :
[quoted text clipped - 14 lines]
>
> Cheers.
Jonathan West - 18 Jan 2005 10:12 GMT
> 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

Signature
Regards
Jonathan West - Word MVP
www.intelligentdocuments.co.uk
Please reply to the newsgroup
Jeff - 18 Jan 2005 12:46 GMT
> 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
> > Perhaps Jonathan West will be along to educate us both further on the
> > wisdom or fallacy of using Set oDoc = ActiveDocument
[quoted text clipped - 23 lines]
> Dim oSelRange As Range
> Set oSelRange = Selection.Range
Greg - 18 Jan 2005 13:04 GMT