How to: Programmatically add headers and footers to documents
Applies to: Visual Studio Visual Studio for Mac
Note
This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here
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 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.
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; }
VSTO 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; }