Why is loop not moving forward?

E

Ed

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
 
J

Jezebel

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
 
E

Ed

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
 
J

Jean-Guy Marcil

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?

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

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


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

Ed

**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
 
E

Ed

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
 
J

Jean-Guy Marcil

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.

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

Jean-Guy Marcil

Ed was telling us:
Ed nous racontait que :
**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

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)?

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

Ed

Thank you. I appreciate the boost.
Ed

Jean-Guy Marcil said:
Ed was telling us:
Ed nous racontait que :


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.

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

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