Word 文書からきれいな HTML とはてな記法で出力する - 経過(1)

現在までに完成している機能

  • 見出し
  • リスト
  • 文中のスタイル変更

これからやる機能

  • Hyperlink処理
  • テーブル処理
  • 画像処理
  • 文書整形

ポイント

処理単位

Word.Document や Word.Range のプロパティにある下記を使えばそれぞれの単位で処理が行える。

プロパティ名 説明
Sections 節ごと
Paragraphs 段落(ENTER改行)ごと
Sentences 一文ごと
Words 単語ごと(日本語文書の場合はあんまり意味ない…)
Characters 一文字ごと

カウントするだけなら Count プロパティをみればいいだけなのでコードに全く意味はないけどサンプル↓

Word.Document document = Globals.ThisAddIn.Application.ActiveDocument;
int countParagraph = 0;
int countSentence = 0;
int countWord = 0;
int countCharacter = 0;
foreach ( Word.Section section in document.Sections )
{
    foreach ( Word.Paragraph paragraph in section.Range.Paragraphs )
    {
        foreach ( Word.Range sentence in paragraph.Range.Sentences )
        {
            foreach ( Word.Range word in sentence.Words )
            {
                foreach ( Word.Range charcter in word.Characters )
                {
                    countCharacter++;
                }
                countWord++;
            }
            countSentence++;
        }
        countParagraph++;
    }
    string msg = string.Format("下記セクションには段落が{0}, 文が{1}, 単語が{2}, 文字が{3} あります。\n---------------------\n{4}", 
        countParagraph, countSentence, countWord, countCharacter, section.Range.Text);
    MessageBox.Show(msg);
}

スタイルの文中変更を確認するためには Characters で一文字ずつみていかないとだめぽい…。DOCX の XML から XSLT で HTML へ変換した方がよいのだろうか…。