Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Word / Programming / January 2005

Tip: Looking for answers? Try searching our database.

Help with paragraph range

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Greg Maxey - 17 Jan 2005 01:01 GMT
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

Signature

Greg Maxey/Word MVP
A Peer in Peer to Peer Support

Jezebel - 17 Jan 2005 01:07 GMT
As discussed in a recent thread(!), screwing around with the contents of a
paragraph seems to bugget up the For each looping.

And trying to edit using Move statements is always fraught ... you're never
completely sure what's going on. Well, I'm not, anyway. Try something
like --

For pIndex = 1 to ActiveDocument.Paragraphs.Count
 With ActiveDocument.Paragraphs(pIndex)
       .Range = Left$(.Range,1)
 End With
Next

> Hello,
>
[quoted text clipped - 35 lines]
>
> End Sub
Greg Maxey - 17 Jan 2005 01:19 GMT
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.

Signature

Greg Maxey/Word MVP
A Peer in Peer to Peer Support

> As discussed in a recent thread(!), screwing around with the contents
> of a paragraph seems to bugget up the For each looping.
[quoted text clipped - 53 lines]
>> Greg Maxey/Word MVP
>> A Peer in Peer to Peer Support
Jonathan West - 17 Jan 2005 11:33 GMT
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
Thanks Jonathan

Rate this thread:






 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.