2

I want to make some modification to the footnotes in a selected portion of a MS Word document. I would like to use a 'for each footnote in selection.footnotes', but this does not work. It returns all the footnotes in the document. I do not understand why, because if I run a 'selection.footnotes.count' this returns only the footnotes in the selected portion, which is what I want. See this code, which can be used for testing the problem.

For example: type a number of lines in Word, insert some footnotes in each line, select one of the lines and run this code to see the difference. Does anyone know what is wrong with my use of the For each footnote ...' statement?

For Each Footnote In Selection.Footnotes
    i = i + 1
Next Footnote
MsgBox "• For ... Next: " & i & Chr(13) & _
"• Count: " & Selection.Footnotes.Count, vbOKOnly, "Number of footnotes in selection, counted using:"
Tim Stack
  • 3,209
  • 3
  • 18
  • 39
James
  • 33
  • 4
  • James, it would help others on the site if you would mark the contribution as an Answer, since, based on your comment, it did solve the problem...? If there is no positive action, eventually this Q&A will disappear. That would be a shame since the issue you discovered could be a very real problem for others, as well. – Cindy Meister Aug 09 '19 at 15:52

1 Answers1

0

I can reproduce what you describe, with the Range as well as the Selection object. I don't know "why"; it makes no sense as far as the object model goes. Sometimes, weird things happen with Selection, but that it also happens with Range...

I'd class it as a bug, but I don't have any confirmation to that effect from Microsoft.

This works, though

For ftCounter = 1 To rng.Footnotes.Count
    Set ft = rng.Footnotes(ftCounter)
    'Debug.Print ft.Range.Text
Next
Cindy Meister
  • 25,071
  • 21
  • 34
  • 43
  • Thank you very much! I'm now using an alternative such as you suggest. – James Jul 24 '19 at 14:00
  • Since you're relatively new to Stack Overflow: remember to mark any contribution that answers a question as "the" answer by clicking the checkmark next to it. That tells everyone the question is taken care of and lets people who may be searching for the same information that the contribution did help. – Cindy Meister Jul 24 '19 at 17:56