2016-09-19 12 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 
関連する問題