Word for 365 ComputeStatistics 宏是否应该识别范围内的 IncludeFootnotesAndEndnotes 参数?

Should Word for 365 ComputeStatistics macro recognize IncludeFootnotesAndEndnotes argument in a Range?

我想编写一个 Word 宏来计算活动文档部分(包括脚注文本)中的字数。

此方法应用于整个文档时可以正常工作:

SectionWordCount = ActiveDocument.ComputeStatistics(Statistic:=wdStatisticWords, _
IncludeFootnotesAndEndnotes:=True)

当此方法在没有 Include 参数的情况下应用于定义为活动部分的范围时,我可以计算活动部分中没有脚注文本的字数:

SectionNum = Selection.Information(wdActiveEndSectionNumber)
Set myRange = ActiveDocument.Sections(SectionNum).Range
SectionWordCount = myRange.ComputeStatistics(Statistic:=wdStatisticWords)

但是,当我尝试将 Include 参数设置为 true 时,此方法失败:

SectionNum = Selection.Information(wdActiveEndSectionNumber)
Set myRange = ActiveDocument.Sections(SectionNum).Range
SectionWordCount = myRange.ComputeStatistics(Statistic:=wdStatisticWords, _
IncludeFootnotesAndEndnotes:=True)

这激怒了'Run-time error 448: Named argument not found.'

有没有人在宏中提供修复或解决方法,以提供活动文档部分的字数统计,包括脚注?

有自己错误的解决方法

没有看到任何回应,我将添加这个尝试的解决方法。由于 IncludeFootnotesAndEndnotes 标志似乎无法用于包含活动部分的文本范围,因此我扩展了宏以遍历每个文档脚注并将这些单独的脚注字数添加到该部分的总数中。但是,这会遇到一个问题,即迭代的脚注不限于活动部分内的脚注,因此最终的字数统计会捕获所有文档脚注(多算)。

那么,如何将迭代限制为活动部分中的脚注?

这是修改后的宏:

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 仅适用于文档级别 (https://docs.microsoft.com/de-de/office/vba/api/word.document.computestatistics) but not on range-level (https://docs.microsoft.com/de-de/office/vba/api/word.range.computestatistics)

VBA 中似乎有错误。 您必须通过索引而不是通过集合来迭代脚注:

    ' 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
```