Search code examples
vbams-wordfootnotes

'selection.footnotes.count' versus 'for each footnote in selection.footnotes'


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:"

Solution

  • 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