How to: Programmatically Add Headers and Footers to Documents

 

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

The procedures are different for document-level customizations and VSTO Add-ins.

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

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

To add text to footers in the document

  1. 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.

                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

  1. 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.

                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;
                }
    

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

To add text to footers in a document

  1. 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.

                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

  1. 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.

                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;
                }
    

How to: Programmatically Create New Documents
How to: Programmatically Extend Ranges in Documents
How to: Programmatically Loop Through Found Items in Documents

Show: