计算Word文档中的单词,包括footnores

时间:2016-09-19 18:38:11

标签: vba ms-word word-count

我会定期收到包含脚注的长篇文档,并试图找到一种方法,使用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参数不可用。

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

1 个答案:

答案 0 :(得分:0)

我认为您需要做的是迭代每个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
相关问题