I want to write a Word macro that counts words in an active document section including footnote text.
This method works correctly when applied to the entire document:
SectionWordCount = ActiveDocument.ComputeStatistics(Statistic:=wdStatisticWords, _
IncludeFootnotesAndEndnotes:=True)
I can count words in an active section without the footnote text when this method, without the Include argument, is applied to a range defined as the active section:
SectionNum = Selection.Information(wdActiveEndSectionNumber)
Set myRange = ActiveDocument.Sections(SectionNum).Range
SectionWordCount = myRange.ComputeStatistics(Statistic:=wdStatisticWords)
However, this method fails when I attempt to set the Include argument true:
SectionNum = Selection.Information(wdActiveEndSectionNumber)
Set myRange = ActiveDocument.Sections(SectionNum).Range
SectionWordCount = myRange.ComputeStatistics(Statistic:=wdStatisticWords, _
IncludeFootnotesAndEndnotes:=True)
This provokes 'Run-time error 448: Named argument not found.'
Does anyone have a fix or work around in a macro that will provide the word count including footnotes for the active document section?
Having seen no response, I will add this attempted workaround. Since the IncludeFootnotesAndEndnotes flag seems inoperable with a text range encompassing the active section, I extended the macro to iterate through each document footnote and add those individual footnote word counts to the total for the section. However, this encounters an issue that the footnotes subject to iteration are not limited to footnotes within the active section, so that the final word count captures all document footnotes (it overcounts).
So, how can I limit the iteration to just the footnotes within the active section?
This is the revised macro:
Sub SectionWordCount()
Dim SectionWordCount As Integer
Dim SectionNum As Integer
Dim f As Footnote
Dim fTempCount As Integer
SectionWordCount = 0
SectionNum = 0
fTempCount = 0
SectionNum = Selection.Information(wdActiveEndSectionNumber)
Set myRange = ActiveDocument.Sections(SectionNum).Range
SectionWordCount = myRange.ComputeStatistics(Statistic:=wdStatisticWords)
' Now get word count in each footnote and accumulate in <fTempCount>
For Each f In myRange.Footnotes
' For some reason Word is iterating over entire document (all footnotes) rather than those associated with the active section.
fTempCount = fTempCount + f.Range.ComputeStatistics(Statistic:=wdStatisticWords)
Next
SectionWordCount = SectionWordCount + fTempCount
MsgBox "Section " & SectionNum & vbCrLf & _
"The current section has " & SectionWordCount & " words including footnotes."
End Sub
Sub SectionWordCount()
Dim SectionWordCount As Integer
Dim SectionNum As Integer
Dim f As Footnote
Dim fTempCount As Integer
SectionWordCount = 0
SectionNum = 0
fTempCount = 0
SectionNum = Selection.Information(wdActiveEndSectionNumber)
Set myRange = ActiveDocument.Sections(SectionNum).Range
SectionWordCount = myRange.ComputeStatistics(Statistic:=wdStatisticWords)
If myRange.Footnotes.Count > 0 Then
For idx = 1 To myRange.Footnotes.Count
Set f = myRange.Footnotes(idx)
fTempCount = fTempCount + f.Range.ComputeStatistics(Statistic:=wdStatisticWords)
Next
End If
SectionWordCount = SectionWordCount + fTempCount
MsgBox "Section " & SectionNum & vbCrLf & _
"The current section has " & SectionWordCount & " words including footnotes."
End Sub
IncludeFootnotesAndEndnotes is only available on document-level (https://learn.microsoft.com/de-de/office/vba/api/word.document.computestatistics) but not on range-level (https://learn.microsoft.com/de-de/office/vba/api/word.range.computestatistics)
There seems to be a bug in VBA. You have to iterate footnotes by index not via the collection:
' Now get word count in each footnote and accumulate in <fTempCount>
If myrange.Footnotes.Count > 0 Then
For i = 1 To myrange.Footnotes.Count
Set f = myrange.Footnotes(i)
' For some reason Word is iterating over entire document (all footnotes) rather than those associated with the active section.
fTempCount = fTempCount + f.Range.ComputeStatistics(Statistic:=wdStatisticWords)
Next
end if
```