次の方法で共有


方法 : 文書にヘッダーおよびフッターを追加する

更新 : 2007 年 11 月

対象

このトピックの情報は、指定された Visual Studio Tools for Office プロジェクトおよび Microsoft Office のバージョンにのみ適用されます。

プロジェクトの種類

  • ドキュメント レベルのプロジェクト

  • アプリケーション レベルのプロジェクト

Microsoft Office のバージョン

  • Word 2003

  • Word 2007

詳細については、「アプリケーションおよびプロジェクトの種類別の使用可能な機能」を参照してください。

文書のヘッダーおよびフッターにテキストを追加するには、SectionHeaders プロパティおよび Footers プロパティを使用します。文書の各セクションには、次に示す 3 つのヘッダーとフッターがあります。

ドキュメント レベルのカスタマイズとアプリケーション レベルのアドインでは、手順が異なります。

ドキュメント レベルのカスタマイズ

文書のフッターにテキストを追加するには

  1. 文書の各セクションのプライマリ フッターに挿入するテキストのフォントを設定します。

    Dim section As Word.Section
    
    For Each section In Me.Sections
        section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _
            .Range.Font.ColorIndex = Word.WdColorIndex.wdDarkRed
    
        section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _
            .Range.Font.Size = 20
    
    foreach (Word.Section wordSection in this.Sections)
    {
        wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
            .Range.Font.ColorIndex = Word.WdColorIndex.wdDarkRed;
    
        wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
            .Range.Font.Size = 20;
    
  2. フッターにテキストを挿入します。

        section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _
            .Range.Text = "Confidential"
    Next
    
        wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
            .Range.Text = "Confidential";
    }
    

文書のヘッダーにテキストを追加するには

  1. 文書の各ヘッダーに "Page X of Y" と表示する AutoText エントリを追加します。

    Dim section As Word.Section
    For Each section In Me.Sections
    
        section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Fields.Add( _
            section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range, _
            Word.WdFieldType.wdFieldEmpty, "AUTOTEXT  ""Page X of Y"" ", True)
    
    foreach (Word.Section section in this.Sections)
    {
        object fieldEmpty = Word.WdFieldType.wdFieldEmpty;
        object autoText = "AUTOTEXT  \"Page X of Y\" ";
        object preserveFormatting = true;
    
        section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Fields.Add(
            section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range,
            ref fieldEmpty, ref autoText, ref preserveFormatting);
    
  2. ヘッダーのテキストが右揃えになるように段落の配置を設定します。

        section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _
            .Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
    Next
    
        section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
            .Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
    }
    

コードのコンパイル

これらのコード例を使用するには、プロジェクトの ThisDocument クラスからコードを実行します。

アプリケーション レベルのアドイン

文書のフッターにテキストを追加するには

  1. 文書の各セクションのプライマリ フッターに挿入するテキストのフォントを設定します。このコード例ではアクティブ文書を使用します。

    Dim section As Word.Section
    Dim document As Word.Document = Me.Application.ActiveDocument
    
    For Each section In document.Sections
        section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _
            .Range.Font.ColorIndex = Word.WdColorIndex.wdDarkRed
    
        section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _
            .Range.Font.Size = 20
    
    Word.Document document = this.Application.ActiveDocument;
    foreach (Word.Section wordSection in document.Sections)
    {
        wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
            .Range.Font.ColorIndex = Word.WdColorIndex.wdDarkRed;
    
        wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
            .Range.Font.Size = 20;
    
  2. フッターにテキストを挿入します。

        section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _
            .Range.Text = "Confidential"
    Next
    
        wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
            .Range.Text = "Confidential";
    }
    

文書のヘッダーにテキストを追加するには

  1. 文書の各ヘッダーに "Page X of Y" と表示する AutoText エントリを追加します。このコード例ではアクティブ文書を使用します。

    Dim section As Word.Section
    For Each section In Me.Application.ActiveDocument.Sections
    
        section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Fields.Add( _
            section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range, _
            Word.WdFieldType.wdFieldEmpty, "AUTOTEXT  ""Page X of Y"" ", True)
    
    foreach (Word.Section section in this.Application.ActiveDocument.Sections)
    {
        object fieldEmpty = Word.WdFieldType.wdFieldEmpty;
        object autoText = "AUTOTEXT  \"Page X of Y\" ";
        object preserveFormatting = true;
    
        section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Fields.Add(
            section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range,
            ref fieldEmpty, ref autoText, ref preserveFormatting);
    
  2. ヘッダーのテキストが右揃えになるように段落の配置を設定します。

        section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _
            .Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
    Next
    
        section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
            .Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
    }
    

コードのコンパイル

これらのコード例を使用するには、プロジェクトの ThisAddIn クラスからコードを実行します。

参照

処理手順

方法 : 新しい文書を作成する

方法 : ドキュメント内の範囲を拡張する

方法 : 文書で見つかった項目をループする