Office関連

Wordマクロで文字数を取得する方法をまとめてみました。

蛍光ペンでマークした部分の文字数をカウントするWordマクロ」で蛍光ペンでマークした部分の文字数をカウントする処理を書きましたが、今回はいくつかある文字数カウント処理方法をまとめてみました。

Public Sub WordCount()
  'DocumentオブジェクトのComputeStatisticsメソッド
  'http://msdn.microsoft.com/en-us/library/office/ff840638%28v=office.15%29.aspx
  '※ 統計情報に脚注の情報を含める場合は引数(IncludeFootnotesAndEndnotes)をTrueにする。
  Debug.Print "■ DocumentオブジェクトのComputeStatisticsメソッド使用"
  Debug.Print "文字数:" & ActiveDocument.ComputeStatistics(Statistic:=wdStatisticCharacters, IncludeFootnotesAndEndnotes:=False)
  Debug.Print "スペースを含めた文字数:" & ActiveDocument.ComputeStatistics(Statistic:=wdStatisticCharactersWithSpaces, IncludeFootnotesAndEndnotes:=False)
  Debug.Print "-----"
  
  'DocumentオブジェクトのBuiltInDocumentPropertiesプロパティ
  'http://msdn.microsoft.com/en-us/library/office/ff196862%28v=office.15%29.aspx
  '※ 引数の詳細は http://msdn.microsoft.com/en-us/library/office/ff195105%28v=office.15%29.aspx 参照
  Debug.Print "■ DocumentオブジェクトのBuiltInDocumentPropertiesプロパティ使用"
  Debug.Print "文字数:" & ActiveDocument.BuiltInDocumentProperties(wdPropertyCharacters)
  Debug.Print "スペースを含めた文字数:" & ActiveDocument.BuiltInDocumentProperties(wdPropertyCharsWSpaces)
  Debug.Print "-----"
  
  'Dialogオブジェクト(wdDialogToolsWordCount)のプロパティ
  '※ 各プロパティの詳細は http://msdn.microsoft.com/en-us/library/office/ff836540%28v=office.15%29 参照
  Selection.Collapse '※ 文字列が選択されていると選択部分のみのカウントになるため選択解除
  Debug.Print "■ Dialogオブジェクト(wdDialogToolsWordCount)のプロパティ使用"
  Debug.Print "文字数:" & Application.Dialogs(wdDialogToolsWordCount).Characters
  Debug.Print "スペースを含めた文字数:" & Application.Dialogs(wdDialogToolsWordCount).CharactersIncludingSpaces
  Debug.Print "-----"
  
  'Dialogオブジェクト(wdDialogDocumentStatistics)のプロパティ
  '※ 各プロパティの詳細は http://msdn.microsoft.com/en-us/library/office/ff836540%28v=office.15%29 参照
  Debug.Print "■ Dialogオブジェクト(wdDialogDocumentStatistics)のプロパティ使用"
  Debug.Print "文字数:" & Application.Dialogs(wdDialogDocumentStatistics).Characters
  Debug.Print "-----"
  
  '特定の範囲の文字カウント
  'http://msdn.microsoft.com/en-us/library/office/ff196924%28v=office.15%29
  Debug.Print "■ 特定の範囲の文字カウント(RangeオブジェクトのComputeStatisticsメソッド使用)"
  Debug.Print "文字数:" & ActiveDocument.Paragraphs(1).Range.ComputeStatistics(Statistic:=wdStatisticCharacters)
  Debug.Print "スペースを含めた文字数:" & ActiveDocument.Paragraphs(1).Range.ComputeStatistics(Statistic:=wdStatisticCharactersWithSpaces)
  Debug.Print "-----"
  
  'Shellオブジェクトを利用してWordの外から単語数取得
  'http://msdn.microsoft.com/en-us/library/windows/desktop/bb787870%28v=vs.85%29.aspx
  '※ 他の方法に比べて不正確
  Debug.Print "■ Shellオブジェクトを利用してWordの外から単語数取得(FolderオブジェクトのGetDetailsOfメソッド使用)"
  With CreateObject("Shell.Application").NameSpace(ThisDocument.Path)
    Debug.Print "単語数:" & .GetDetailsOf(.ParseName(ThisDocument.Name), 151)
  End With
  Debug.Print "-----"
End Sub

ComputeStatisticsメソッドやBuiltInDocumentPropertiesプロパティから文字数を取得する方法があるので、ケースバイケースで使い分けるのが良いでしょう。
上記コードではオマケとして、Shellオブジェクトを利用してWordの外から単語数を取得する方法も書きましたが、他の方法に比べると正確性に欠けるため、使用する機会は無いだろうと思います。

蛍光ペンでマークした部分の文字数をカウントするWordマクロ前のページ

「NetOffice」で簡単に.NETからOfficeを操作次のページ

関連記事

  1. Office関連

    代替テキストを削除するPowerPointマクロ

    PowerPointの図やSmartArt、グループやグラフといった視…

  2. Office アドイン

    [Officeアドイン]マニフェストファイルをデバッグする方法

    Office アドイン本体はF12ツール等を使ってデバッグすることがで…

  3. Office関連

    Google Docs APIを使ってGoogle ドキュメントを操作するVBAマクロ

    前回の記事で新しく追加されたGoogle Docs APIを紹介しまし…

  4. アイコン一覧

    Office 2013 アイコン一覧(C)

    ・Office 2013 アイコン一覧 NUM…

  5. Office関連

    マクロに割り当てたショートカットキーをCSVファイルとして出力するWordマクロ

    Word MVPの新田さんが書かれた以下の記事を見て思いついたマクロで…

  6. Office関連

    Office 2013 オンラインヘルプのリンクを集めてみました。

    新機能を把握するためにはヘルプを見るのが一番早い、というわけでOffi…

コメント

  1. この記事へのコメントはありません。

  1. この記事へのトラックバックはありません。

Time limit is exhausted. Please reload CAPTCHA.

※本ページはプロモーションが含まれています。

Translate

最近の記事

アーカイブ

PAGE TOP