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 / March 2006

Tip: Looking for answers? Try searching our database.

Why is loop not moving forward?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ed - 02 Mar 2006 21:39 GMT
This loop works - but it stays in one place and doesn't move forward through
the document.  What simple silly thing have I forgotten?

With doc.Content.Find
   .ClearFormatting
   .Style = "TOCpageref"
   .Wrap = wdFindContinue
   Do While .Execute(Forward:=True, _
         Format:=True) = True

       Selection.Collapse wdCollapseStart

       Set fldPgNo = doc.Fields.Add(Range:=Selection.Range, _
           Type:=wdFieldPage)
       strPgNo = fldPgNo.Result
       fldPgNo.Delete
       MsgBox strPgNo
   Loop
End With
Jezebel - 02 Mar 2006 21:57 GMT
It's not that it doesn't move forward, but that your "collapseStart"
instruction is taking you back to the beginning each time. You need to read
help on the difference between running Find using the Selection object, and
using it with a Range object. You're currently doing a bit of both. It
should be something like --

Dim pRange as Word.Range
Set pRange = doc.Content

With prange.Find
  .ClearFormatting
  .Style = "TOCpageref"
  .Wrap = wdFindContinue
   .Format = true
   .Forward = true
  Do While .Execute
      Set fldPgNo = doc.Fields.Add(Range:=pRange, Type:=wdFieldPage)
      strPgNo = fldPgNo.Result
      fldPgNo.Delete
      MsgBox strPgNo
  Loop
End With

> This loop works - but it stays in one place and doesn't move forward
> through
[quoted text clipped - 16 lines]
>    Loop
> End With
Ed - 02 Mar 2006 22:04 GMT
> It's not that it doesn't move forward, but that your "collapseStart"
> instruction is taking you back to the beginning each time.
D'oh!
Thanks for the boost!

Ed

> It's not that it doesn't move forward, but that your "collapseStart"
> instruction is taking you back to the beginning each time. You need to read
[quoted text clipped - 39 lines]
> >    Loop
> > End With
Ed - 02 Mar 2006 22:37 GMT
**PROBLEM**
As it was, your code inserted the field to encompass the entire range.
Thus, when the field was deleted, it _also_ deleted the encompassed range of
formatted text.  If I tried to used a wdCollapseEnd, the code went into an
endless loop.  I changed it to create a duplicate range, so I could collapse
that one and not mess with the original range, but it still went into an
endless loop.  Here's my modifications - any joy forthcoming?

Ed

Dim pRange As Range
Dim cRange As Range
Set pRange = doc.Content

With pRange.Find
  .ClearFormatting
  .Style = "TOCpageref"
  .Wrap = wdFindContinue
  .Format = True
  .Forward = True
  Do While .Execute
      Set cRange = pRange.Duplicate
      cRange.Collapse wdCollapseEnd
      Set fldPgNo = doc.Fields.Add _
        (Range:=cRange, Type:=wdFieldPage)
      strPgNo = fldPgNo.Result
      fldPgNo.Delete
      MsgBox strPgNo
  Loop
End With

> It's not that it doesn't move forward, but that your "collapseStart"
> instruction is taking you back to the beginning each time. You need to read
[quoted text clipped - 39 lines]
> >    Loop
> > End With
Jean-Guy Marcil - 03 Mar 2006 16:18 GMT
Ed was telling us:
Ed nous racontait que :

> **PROBLEM**
> As it was, your code inserted the field to encompass the entire range.
[quoted text clipped - 27 lines]
>   Loop
> End With

Just a quick thought:
Have you tried replacing
>   .Wrap = wdFindContinue
by
   .Wrap = wdFindStop
???

Also, shouldn't you be using
   cRange.Collapse wdCollapseStart
instead of
>       cRange.Collapse wdCollapseEnd
unless you want the page number for the end of the range (in cases where the
range spans more than one page)?

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org 

Jean-Guy Marcil - 02 Mar 2006 22:08 GMT
Ed was telling us:
Ed nous racontait que :

> This loop works - but it stays in one place and doesn't move forward
> through the document.  What simple silly thing have I forgotten?
[quoted text clipped - 15 lines]
>    Loop
> End With

Two problems that I can see:

1)
>    .Wrap = wdFindContinue
will cause an infinite loop.
2)
>        Selection.Collapse wdCollapseStart.
refers to what is currently selected in the document, not what is actually
found by the find operation.

Try this code:

Dim Doc As Document
Dim fldPgNo As Field
Dim strPgNo As String

Dim startRange As Range

Set startRange = Selection.Range

Set Doc = ActiveDocument

With Doc.Content.Find
   .ClearFormatting
   .Style = "TOCpageref"
   .Wrap = wdFindStop

   Do While .Execute(Forward:=True, _
         Format:=True) = True

       .Parent.Select
       Selection.Collapse wdCollapseStart

       Set fldPgNo = Doc.Fields.Add(Range:=Selection.Range, _
           Type:=wdFieldPage)
       strPgNo = fldPgNo.Result
       fldPgNo.Delete
       MsgBox strPgNo
   Loop
End With

startRange.Select

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org 

Ed - 02 Mar 2006 22:50 GMT
Excellent, Jean-Guy!  Thank you!
So the .Parent.Select takes me out of the Range object back into the
document itself, essentially into the Selection object of the text of the
range?

Ed

> Ed was telling us:
> Ed nous racontait que :
[quoted text clipped - 61 lines]
>
> startRange.Select
Jean-Guy Marcil - 03 Mar 2006 16:13 GMT
Ed was telling us:
Ed nous racontait que :

> Excellent, Jean-Guy!  Thank you!
> So the .Parent.Select takes me out of the Range object back into the
> document itself, essentially into the Selection object of the text of
> the range?

I guess you could say it like that.

I would say that the Parent.Select line selects the text in the range that
was found by the .Find, so you are not "taken out of the range", but you
select it and create a Selection object from that range.

I could have rewritten your code to use only the range object, but I thought
it would be easier to just modify your code as little as possible to show
you how to use the selection object in this case.

Signature

Salut!
_______________________________________
Jean-Guy Marcil - Word MVP
jmarcilREMOVE@CAPSsympatico.caTHISTOO
Word MVP site: http://www.word.mvps.org 

Ed - 06 Mar 2006 13:23 GMT
Thank you.  I appreciate the boost.
Ed

> Ed was telling us:
> Ed nous racontait que :
[quoted text clipped - 13 lines]
> it would be easier to just modify your code as little as possible to show
> you how to use the selection object in this case.
 
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.