Dela via


How to: Add Headers and Footers to Documents

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Document-level projects

  • Application-level projects

Microsoft Office version

  • Word 2003

  • Word 2007

For more information, see Features Available by Application and Project Type.

You can add text to headers and footers in your document by using the Headers property and Footers property of the Section. Each section of a document contains three headers and footers:

The procedures are different for document-level customizations and application-level add-ins.

Document-Level Customizations

To add text to footers in the document

  1. Set the font of the text to be inserted into the primary footer of each section of the document.

    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. Insert the text into the footer.

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

To add text to headers in the document

  1. Add an AutoText entry to show Page X of Y in each header in the document.

    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. Set the paragraph alignment so that the text aligns to the right of the header.

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

Compiling the Code

To use these code examples, run them from the ThisDocument class in your project.

Application-Level Add-Ins

To add text to footers in a document

  1. Set the font of the text to be inserted into the primary footer of each section of the document. This code example uses the active document.

    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. Insert the text into the footer.

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

To add text to headers in the document

  1. Add an AutoText entry to show Page X of Y in each header in the document. This code example uses the active document.

    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. Set the paragraph alignment so that the text aligns to the right of the header.

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

Compiling the Code

To use these code examples, run them from the ThisAddIn class in your project.

See Also

Tasks

How to: Create New Documents

How to: Extend Ranges in Documents

How to: Loop Through Found Items in Documents