Click to Rate and Give Feedback
MSDN
MSDN Library
Visual Studio 2008
Visual Studio
Word Solutions
 How to: Add Headers and Footers to ...

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
Microsoft Visual Studio Tools for the Microsoft Office system (version 3.0)
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.

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.

    Visual Basic
    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
    
    
    C#
    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.

    Visual Basic
        section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _
            .Range.Text = "Confidential"
    Next
    
    
    C#
        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.

    Visual Basic
    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)
    
    
    C#
    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.

    Visual Basic
        section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _
            .Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
    Next
    
    
    C#
        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.

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.

    Visual Basic
    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
    
    
    C#
    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.

    Visual Basic
        section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _
            .Range.Text = "Confidential"
    Next
    
    
    C#
        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.

    Visual Basic
    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)
    
    
    C#
    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.

    Visual Basic
        section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _
            .Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
    Next
    
    
    C#
        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.

Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker