Putting a footnote reference into a field result

M

mccaskey

I want a (well-behaved) field whose result can be a reference to a
footnote.

I found, with a little sidestep, I can do it with the (barely
documented and poorly behaved) { ADDIN } field as follows:

Sub insertNote()

' create the field
Set fField = ActiveDocument.Fields.Add(Selection.Range,
wdFieldQuote, "text")
fField.Code.Text = " ADDIN "
fField.Result.Text = ""

' add the footnote
Set note = ActiveDocument.Footnotes.Add(Range:=fField.Result)
note.Range.Text = "Hello"

End Sub

Run this and you have a field whose result is a reference to a
footnote whose content is "Hello". Cool.

But alas, all is not well. Manually add another footnote to the
document. Put "Goodbye" into the footnote.

Now run this routine:

Sub scanNote()

MsgBox ActiveDocument.Fields(1).Result.Footnotes.Count

For Each myfootnote In ActiveDocument.Fields(1).Result.Footnotes
MsgBox myfootnote.Range.Text
Next myfootnote

End Sub

The first MsgBox says that there is one footnote in the result of the
field. That's right.

But the loop then iterates through all footnotes in the whole
document. That's wrong. This seems a bug in ADDIN, but since that
field is a secret of sorts anyway (and maybe I wasn't supposed to be
able to put a note reference in it in the first place), I guess I
can't complain.

My problem is that given the field, I need to go look at the content
of the footnote, but I can't.

Any suggestions appreciated. (I'm using Word 2003 but I need this to
work with newer versions also.)
 
J

Jean-Guy Marcil

mccaskey was telling us:
mccaskey nous racontait que :
I want a (well-behaved) field whose result can be a reference to a
footnote.

I found, with a little sidestep, I can do it with the (barely
documented and poorly behaved) { ADDIN } field as follows:

Sub insertNote()

' create the field
Set fField = ActiveDocument.Fields.Add(Selection.Range,
wdFieldQuote, "text")
fField.Code.Text = " ADDIN "
fField.Result.Text = ""

' add the footnote
Set note = ActiveDocument.Footnotes.Add(Range:=fField.Result)
note.Range.Text = "Hello"

End Sub

Run this and you have a field whose result is a reference to a
footnote whose content is "Hello". Cool.

But alas, all is not well. Manually add another footnote to the
document. Put "Goodbye" into the footnote.

Now run this routine:

Sub scanNote()

MsgBox ActiveDocument.Fields(1).Result.Footnotes.Count

For Each myfootnote In ActiveDocument.Fields(1).Result.Footnotes
MsgBox myfootnote.Range.Text
Next myfootnote

End Sub

The first MsgBox says that there is one footnote in the result of the
field. That's right.

But the loop then iterates through all footnotes in the whole
document. That's wrong. This seems a bug in ADDIN, but since that
field is a secret of sorts anyway (and maybe I wasn't supposed to be
able to put a note reference in it in the first place), I guess I
can't complain.

My problem is that given the field, I need to go look at the content
of the footnote, but I can't.

Any suggestions appreciated. (I'm using Word 2003 but I need this to
work with newer versions also.)

Actually, it has nothing to do with the fact that you are using a weird
Field/footnote combination.

It is a bit of a normal result because the line:
For Each myfootnote In ActiveDocument.Fields(1).Result.Footnotes
returns all footnotes in the document, regardless of the fact that you are
using a field. You would get the same result with:
For Each myfootnote In ActiveDocument.Paragraphs(1).Range.Footnotes
I do not know why, but once you access a footnote item in a selection or a
range, Word will cycle through the full collection.
You have to force it to stay within the range sub-collection, like in the
following code:

'_______________________________________
Sub scanNote()

Dim myfootnote As Footnote
Dim i As Long

With ActiveDocument.Fields(1).Result.Footnotes
MsgBox .Count

For i = 1 To .Count
MsgBox .Item(i).Range.Text
Next
End With

End Sub
'_______________________________________

--

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

mccaskey

mccaskey was telling us:
mccaskey nous racontait que :




















Actually, it has nothing to do with the fact that you are using a weird
Field/footnote combination.

It is a bit of a normal result because the line:
For Each myfootnote In ActiveDocument.Fields(1).Result.Footnotes
returns all footnotes in the document, regardless of the fact that you are
using a field. You would get the same result with:
For Each myfootnote In ActiveDocument.Paragraphs(1).Range.Footnotes
I do not know why, but once you access a footnote item in a selection or a
range, Word will cycle through the full collection.
You have to force it to stay within the range sub-collection, like in the
following code:

'_______________________________________
Sub scanNote()

Dim myfootnote As Footnote
Dim i As Long

With ActiveDocument.Fields(1).Result.Footnotes
MsgBox .Count

For i = 1 To .Count
MsgBox .Item(i).Range.Text
Next
End With

End Sub
'_______________________________________

--

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

Well that is definitely weird.
If I
Set myNotes = ActiveDocument.Paragraphs(1).Result.Footnotes
and browse the contents of myNotes using the Locals window, I see Item
1 (whose index is 1), Item 2 (whose index is 2), etc. And if I
iterate, I get Item 1, Item 2, etc. But
ActiveDocument.Paragraphs(1).Result.Footnotes.Item(1)
returns Item 2 since it was the first footnote in the paragraph!?

Does Microsoft call this a bug or an undocumented feature? Sheesh.

Jean-Guy, when the new Word plug-in for Zotero (www.zotero.org) comes
out, you may know that you helped make it possible, having gotten me
out of two jams now.

Thanks!
John
 
J

Jean-Guy Marcil

mccaskey was telling us:
mccaskey nous racontait que :
Well that is definitely weird.
If I
Set myNotes = ActiveDocument.Paragraphs(1).Result.Footnotes
and browse the contents of myNotes using the Locals window, I see Item
1 (whose index is 1), Item 2 (whose index is 2), etc. And if I
iterate, I get Item 1, Item 2, etc. But
ActiveDocument.Paragraphs(1).Result.Footnotes.Item(1)
returns Item 2 since it was the first footnote in the paragraph!?

Does Microsoft call this a bug or an undocumented feature? Sheesh.

A semi-bug?
Jean-Guy, when the new Word plug-in for Zotero (www.zotero.org) comes
out, you may know that you helped make it possible, having gotten me
out of two jams now.

Does that qualify me for a complimentary copy?
;-)

--

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

mccaskey

mccaskey was telling us:
mccaskey nous racontait que :



A semi-bug?


Does that qualify me for a complimentary copy?
;-)

--

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

A complimentary copy of some free downloadable software? Sure. Go
right ahead. Take two! You've earned it! :)
 
J

Jean-Guy Marcil

mccaskey was telling us:
mccaskey nous racontait que :
A complimentary copy of some free downloadable software? Sure. Go
right ahead. Take two! You've earned it! :)

LOL
Darn, here I thought I would be getting some special treatment!

--

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