2016-09-19 35 views
0

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

我有正确计算在正文中单词的数量,使用命令宏:

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

变量POS1和POS2已设置为页面的第一个和最后一个字符正在计数。

然而,当我真参数添加到ComputeStatistics(wdStatisticWords,真),以IncludeFootnotesAndEndnotes,如:

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

它不工作,给了一个错误,有太多的参数。看起来在使用Range时,IncludeFootnotesAndEndnotes参数不可用。

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

回答

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