Partager via


How to: Programmatically Add Headers and Footers to Documents

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.

Applies to: The information in this topic applies to document-level projects and application-level projects for Word 2013 and Word 2010. For more information, see Features Available by Office Application and Project Type.

Document-Level Customizations

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

To add text to footers in the document

  • The following code example sets the font of the text to be inserted into the primary footer of each section of the document, and then inserts text into the footer.

    For Each section As Word.Section In Me.Sections
        Dim footerRange As Word.Range = section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
        footerRange.Font.ColorIndex = Word.WdColorIndex.wdDarkRed
        footerRange.Font.Size = 20
        footerRange.Text = "Confidential" 
    Next
    
    foreach (Word.Section wordSection in this.Sections)
    {
        Word.Range footerRange = wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
        footerRange.Font.ColorIndex = Word.WdColorIndex.wdDarkRed;
        footerRange.Font.Size = 20;
        footerRange.Text = "Confidential";
    }
    

To add text to headers in the document

  • The following code example adds a field to show the page number in each header in the document, and then sets the paragraph alignment so that the text aligns to the right of the header.

    For Each section As Word.Section In Me.Sections
        Dim headerRange As Word.Range = section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
        headerRange.Fields.Add(headerRange, Word.WdFieldType.wdFieldPage)
        headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
    Next
    
    foreach (Word.Section section in this.Sections)
    {
        Word.Range headerRange = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
        headerRange.Fields.Add(headerRange, Word.WdFieldType.wdFieldPage);
        headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
    }
    

Application-Level Add-Ins

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

To add text to footers in a document

  • The following code example sets the font of the text to be inserted into the primary footer of each section of the document, and then inserts text into the footer. This code example uses the active document.

    For Each section As Word.Section In Me.Application.ActiveDocument.Sections
        Dim footerRange As Word.Range = section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
        footerRange.Font.ColorIndex = Word.WdColorIndex.wdDarkRed
        footerRange.Font.Size = 20
        footerRange.Text = "Confidential" 
    Next
    
    foreach (Word.Section wordSection in this.Application.ActiveDocument.Sections)
    {
        Word.Range footerRange = wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
        footerRange.Font.ColorIndex = Word.WdColorIndex.wdDarkRed;
        footerRange.Font.Size = 20;
        footerRange.Text = "Confidential";
    }
    

To add text to headers in the document

  • The following code example adds a field to show the page number in each header in the document, and then sets the paragraph alignment so that the text aligns to the right of the header. This code example uses the active document.

    For Each section As Word.Section In Me.Application.ActiveDocument.Sections
        Dim headerRange As Word.Range = section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range
        headerRange.Fields.Add(headerRange, Word.WdFieldType.wdFieldPage)
        headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight
    Next
    
    foreach (Word.Section section in this.Application.ActiveDocument.Sections)
    {
        Word.Range headerRange = section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
        headerRange.Fields.Add(headerRange, Word.WdFieldType.wdFieldPage);
        headerRange.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
    }
    

See Also

Tasks

How to: Programmatically Create New Documents

How to: Programmatically Extend Ranges in Documents

How to: Programmatically Loop Through Found Items in Documents