You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Word 365 ComputeStatistics的Range调用支持IncludeFootnotesAndEndnotes吗

Word VBA统计当前章节含脚注总字数的问题及解决方案

问题背景

需要编写VBA宏统计Word活动文档中当前章节包含脚注在内的总字数,调用ComputeStatistics方法时出现参数不兼容问题。

问题细节

  • 文档级调用ComputeStatistics正常
    对整个ActiveDocument对象调用该方法时,可以正常传入IncludeFootnotesAndEndnotes参数实现含脚注的字数统计:
SectionWordCount = ActiveDocument.ComputeStatistics(Statistic:=wdStatisticWords, _
IncludeFootnotesAndEndnotes:=True)
  • Range级调用不传脚注参数正常
    对当前章节对应的Range对象调用该方法时,不传入脚注相关参数,可以正常统计章节正文字数:
SectionNum = Selection.Information(wdActiveEndSectionNumber)
Set myRange = ActiveDocument.Sections(SectionNum).Range
SectionWordCount = myRange.ComputeStatistics(Statistic:=wdStatisticWords)
  • Range级传入脚注参数报错
    如果对Range对象调用该方法时传入IncludeFootnotesAndEndnotes:=True参数,会触发「运行时错误448:未找到命名参数」报错,对应代码如下:
SectionNum = Selection.Information(wdActiveEndSectionNumber)
Set myRange = ActiveDocument.Sections(SectionNum).Range
SectionWordCount = myRange.ComputeStatistics(Statistic:=wdStatisticWords, _
IncludeFootnotesAndEndnotes:=True)

初始缺陷方案

最初尝试通过遍历脚注累加字数解决参数不兼容问题,但For Each循环会遍历整个文档的所有脚注,导致统计结果偏高,缺陷代码如下:

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)
    
    ' 遍历脚注累加字数,此处存在问题:Word会遍历全文档所有脚注,而非仅当前章节的脚注
    For Each f In myRange.Footnotes
       fTempCount = fTempCount + f.Range.ComputeStatistics(Statistic:=wdStatisticWords)
    Next
   
   SectionWordCount = SectionWordCount + fTempCount
   
   MsgBox "第" & SectionNum & "节" & vbCrLf & _
   "当前章节含脚注总字数为:" & SectionWordCount

End Sub

最终可用方案

调整遍历逻辑,改为按索引遍历当前Range下的脚注即可解决问题,完整可运行宏代码如下:

Sub SectionWordCount()
   Dim SectionWordCount As Integer
   Dim SectionNum As Integer
   Dim f As Footnote
   Dim fTempCount As Integer
   Dim idx 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 "第" & SectionNum & "节" & vbCrLf & _
   "当前章节含脚注总字数为:" & SectionWordCount
  
End Sub

内容的提问来源于stack exchange,提问作者Adam

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.02 17:06:02