For Each doesn't work with Footnotes?

B

Boutang

For a W95-style help project, I'm trying to print an ordered list of document
footnotes. I've discovered what seems to be a bug in the Footnotes
collection.

Specifically, if you use For Each-Next with .Range.Footnotes, you get all
footnotes in the document instead of all footnotes in the range you've
specified.

Can anyone else confirm, or point out where my code is wrong?

Thanks.

Here's my code, commented where I think the bug is:

For Each ftnTopic In Documents(pstrDoc).Footnotes
If ftnTopic.Reference.Text = "#" Then 'It's a context ID
strTitle = ""
strBrowse = ""
'Get Topic Title and Browse Sequence

'I think this next line is buggy. For my document,
'.Range.Footnotes.Count=4, yet the For Each construct
'loops through every footnote in the document. This
'wrecks the values put into strTitle & strBrowse

For Each ftn In _
ftnTopic.Reference.Paragraphs(1).Range.Footnotes

If ftn.Reference.Text = "$" Then 'Topic Title
strTitle = ftn.Range.Text
End If
If ftn.Reference.Text = "+" Then 'Browse Sequence
strBrowse = ftn.Range.Text
End If
Next ftn

Documents(pstrDocNew).Range.InsertAfter """" _
& pstrDoc & """, """ & ftnTopic.Range.Text & """, """ _
& strTitle & """, """ & strBrowse & """" & vbCrLf
End If
Next ftnTopic
 
D

Doug Robbins

It's not a problem with For Each. It must be something else with your code.

The following works with both footnotes and endnotes as long as the object
type and references are changed as appropriate.

' Macro created 29/09/99 by Doug Robbins to replace endnotes with textnotes
at end of document

' to replace the endnote reference in the body of the document with a
superscript number.

'

Dim aendnote As Endnote

For Each aendnote In ActiveDocument.Endnotes

ActiveDocument.Range.InsertAfter vbCr & aendnote.Index & vbTab &
aendnote.Range

aendnote.Reference.InsertBefore "a" & aendnote.Index & "a"

Next aendnote

For Each aendnote In ActiveDocument.Endnotes

aendnote.Reference.Delete

Next aendnote

Selection.Find.ClearFormatting

Selection.Find.Replacement.ClearFormatting

With Selection.Find.Replacement.Font

.Superscript = True

End With

With Selection.Find

.Text = "(a)([0-9]{1,})(a)"

.Replacement.Text = "\2"

.Forward = True

.Wrap = wdFindContinue

.Format = True

.MatchWildcards = True

End With

Selection.Find.Execute Replace:=wdReplaceAll
--
Please respond to the Newsgroup for the benefit of others who may be
interested. Questions sent directly to me will only be answered on a paid
consulting basis.

Hope this helps,
Doug Robbins - Word MVP
 
B

Boutang

Thanks for your post, Doug. Actually, the problem IS with For Each, or at
least with the way For Each interacts with the Footnotes collection when that
collection is referenced via the following call:

For Each ftnTopic In ActiveDocument.Footnotes
For Each ftn In ftnTopic.Reference.Paragraphs(1).Range.Footnotes
'do something here
Next ftn
Next ftnTopic

(In other words, look at an existing footnote, find the paragraph that it's
in, then take THAT paragraph's Footnotes collection.)

I'm pretty sure the problem is with For Each because Access 2.0-style coding
works just fine in the "inner" Footnotes loop, as follows:

For Each ftnTopic In ActiveDocument.Footnotes
For intCounter = 1 To ftnTopic.Reference.Paragraphs(1).Range.Footnotes.Count
Set ftn = ftnTopic.Reference.Paragraphs(1).Range.Footnotes(intCounter)
'put code here to work with invidual footnotes
Next intCounter
Next ftnTopic

I think Word is having problems dealing with two Footnotes collections at
once: one for the current document, and then one for a particular paragraph.
 

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