【问题标题】:Counting words in Word document, including footnores计算 Word 文档中的单词,包括脚注
【发布时间】:2016-09-19 18:38:10
【问题描述】:

我定期收到包含脚注的长文档,并试图找到一种使用 VBA 计算每页上的字数(包括脚注)的方法。脚注是否溢出到下一页并不重要,我只是计算字数,包括锚定在页面上的脚注。

我有一个宏可以正确计算文本正文中的单词数,使用以下命令:

WordCount = ActiveDocument.Range(Start:=pos1, End:=pos2).ComputeStatistics(wdStatisticWords)

变量 pos1 和 pos2 已设置为被统计页面的第一个和最后一个字符。

但是,当我将 True 参数添加到 ComputeStatistics(wdStatisticWords, True) 时,添加到 IncludeFootnotesAndEndnotes,如下所示:

WordCount = ActiveDocument.Range(Start:=pos1, End:=pos2).ComputeStatistics(wdStatisticWords, True)

它不起作用,给出参数太多的错误。看来使用 Range 时,IncludeFootnotesAndEndnotes 参数不可用。

如何计算范围内脚注中的单词数?

【问题讨论】:

    标签: vba ms-word word-count


    【解决方案1】:

    我认为您需要做的是迭代每个 StoryRanges 并更新一个计数器。这是一个应该作为示例的小示例,但是,您可能需要针对您的具体情况对其进行调整(查看我关于 StoryRanges 枚举的注释)

    代码如下:

    Public Sub Count_All_Words()
        Dim Story_Ranges         As Variant: Set Story_Ranges = ActiveDocument.StoryRanges
        Dim Story_Range          As Object
        Dim WordCount            As Long
    
        'Loop through each story range and only include the footer and Main story to the word count
        For Each Story_Range In Story_Ranges
            'You may need to check additional types, lookup the enumerations for StoryType here:
            'https://msdn.microsoft.com/en-us/library/bb238219(v=office.12).aspx
            If Story_Range.StoryType = wdMainTextStory Or Story_Range.StoryType = wdFootnoteSeparatorStory Then
                'Add to the word count
                WordCount = WordCount + Story_Range.ComputeStatistics(wdStatisticWords)
            End If
        Next
    
        Debug.Print "The word count is: " & WordCount
    End Sub
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-05-28
      • 1970-01-01
      • 2018-03-04
      • 2014-05-12
      • 1970-01-01
      • 2022-07-25
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多