Microsoft KB Archive/269565

= How to automate Word to set and retrieve section header and footer information =

Article ID: 269565

Article Last Modified on 4/16/2007

-

APPLIES TO


 * Microsoft Office Word 2007
 * Microsoft Office Word 2003
 * Microsoft Word 2002 Standard Edition
 * Microsoft Word 2000 Standard Edition
 * Microsoft Visual Basic 5.0 Professional Edition
 * Microsoft Visual Basic 6.0 Professional Edition
 * Microsoft Visual Basic 5.0 Enterprise Edition
 * Microsoft Visual Basic 6.0 Enterprise Edition

-



This article was previously published under Q269565



SUMMARY
This article illustrates how to automate Microsoft Word to set and retrieve header and footer text in various sections of a document.



MORE INFORMATION
The sample automation client first demonstrates how to build sections in a document. Then, the sample demonstrates how to add header and footer text to those sections. The document that the sample code creates consists of three sections:
 * The first section contains two pages. In this section, the first page header and footer are different from the second page.
 * The second section contains three pages. In this section, the first page header and footer are different from the other two pages.
 * The third section contains two pages. In this section, the pages all share the same header and footer.

NOTE: Whether or not a section contains different headers or footers for the first page is determined by the DifferentFirstPageHeaderFooter property for the Section object.

The sample automation client also demonstrates how to retrieve the header and footer information from a document. After opening a document, the client iterates each page in the document and outputs the headers and footers for that page to the debug window. Note that the sample code accounts only for first page headers/footers and primary headers/footers; it does not account for even page headers/footers.

Sample
 Create a new Standard EXE Project in Visual Basic. Form1 is created by default. On the Project menu, click References. Click one of the following, and then click OK:  For Word 2007, click Microsoft Word 12.0 Object Library. For Word 2003, click Microsoft Word 11.0 Object Library. For Word 2002, click Microsoft Word 10.0 Object Library. For Word 2000, click Microsoft Word 9.0 Object Library.</ul> </li> Add two Command buttons to Form1. Change the caption of Command1 to Create Document, and change the caption of Command2 to Retrieve Headers/Footers .</li>  Add the following code to Form1: Option Explicit

Private Sub Command1_Click SetHeadersFooters End Sub

Private Sub Command2_Click GetHeadersFooters App.Path & &quot;\mydoc.doc&quot; End Sub

Sub SetHeadersFooters

Dim oApp As Word.Application Dim oSec As Word.Section Dim oDoc As Word.Document 'Create a new document in Word Set oApp = New Word.Application Set oDoc = oApp.Documents.Add With oDoc '=== SECTION 1 ================================================== 'Add two pages to the first section where the first page in the 'section has different headers and footers than the second page Set oSec = .Sections(1) oSec.PageSetup.DifferentFirstPageHeaderFooter = True oSec.Range.InsertAfter &quot;Text on Page 1 (Section 1)&quot; .Range(oSec.Range.End - 1).InsertBreak wdPageBreak oSec.Range.InsertAfter &quot;Text on Page 2 (Section 1)&quot; 'Add the headers/footers for the first section (that contains two       'pages) oSec.Headers(wdHeaderFooterFirstPage).Range.Text = _ &quot;Page1 -- Section 1 First Page Header&quot; oSec.Headers(wdHeaderFooterPrimary).Range.Text = _ &quot;Page2 -- Section 1 Primary Header&quot; oSec.Footers(wdHeaderFooterFirstPage).Range.Text = _ &quot;Page1 -- Section 1 First Page Footer&quot; oSec.Footers(wdHeaderFooterPrimary).Range.Text = _ &quot;Page2 -- Section 1 Primary Footer&quot; '=== SECTION 2 ================================================== 'Add a new section containing three pages where the first page in        'the section has different headers and footers than the other two 'pages .Range(oSec.Range.End - 1).InsertBreak wdSectionBreakNextPage Set oSec = .Sections(2) oSec.PageSetup.DifferentFirstPageHeaderFooter = True oSec.Range.InsertAfter &quot;Text on Page 3 (Section 2)&quot; .Range(oSec.Range.End - 1).InsertBreak wdPageBreak oSec.Range.InsertAfter &quot;Text on Page 4 (Section 2)&quot; .Range(oSec.Range.End - 1).InsertBreak wdPageBreak oSec.Range.InsertAfter &quot;Text on Page 5 (Section 2)&quot;

'Add the headers/footers for the second section (that contains       'three pages) -- notice that the second and third pages in this 'section will contain the primary header/footer oSec.Headers(wdHeaderFooterFirstPage).LinkToPrevious = False oSec.Headers(wdHeaderFooterFirstPage).Range.Text = _ &quot;Page3 -- Section 2 First Page Header&quot; oSec.Headers(wdHeaderFooterPrimary).LinkToPrevious = False oSec.Headers(wdHeaderFooterPrimary).Range.Text = _ &quot;Page4and5 -- Section 2 Primary Header&quot; oSec.Footers(wdHeaderFooterFirstPage).LinkToPrevious = False oSec.Footers(wdHeaderFooterFirstPage).Range.Text = _ &quot;Page3 -- Section 2 First Page Footer&quot; oSec.Footers(wdHeaderFooterPrimary).LinkToPrevious = False oSec.Footers(wdHeaderFooterPrimary).Range.Text = _ &quot;Page4and5 -- Section 2 Primary Footer&quot; '=== SECTION 3 ==================================================

'Add a new section containing two pages that all have the same 'header/footer .Range(oSec.Range.End - 1).InsertBreak wdSectionBreakNextPage Set oSec = .Sections(3) oSec.PageSetup.DifferentFirstPageHeaderFooter = False oSec.Range.InsertAfter &quot;Text on Page 6 (Section 3)&quot; .Range(oSec.Range.End - 1).InsertBreak wdPageBreak oSec.Range.InsertAfter &quot;Text on Page 7 (Section 3)&quot;

'Add the headers/footers for the third section (that contains       ' two pages) oSec.Headers(wdHeaderFooterPrimary).LinkToPrevious = False oSec.Headers(wdHeaderFooterPrimary).Range.Text = _ &quot;Page6and7 -- Section 3 Primary Header Only&quot; oSec.Footers(wdHeaderFooterPrimary).LinkToPrevious = False oSec.Footers(wdHeaderFooterPrimary).Range.Text = _ &quot;Page6and7 -- Section 3 Primary Footer Only&quot; 'Save the document .SaveAs App.Path & &quot;\mydoc.doc&quot; End With 'Make Word visible to examine the document oApp.Visible = True End Sub

Sub GetHeadersFooters(sFile As String)

Dim oApp As Word.Application Dim oDoc As Word.Document Dim oSec As Word.Section Dim oPageStart As Word.Range Dim iPage As Integer, iTotalPages As Integer, iSection As Integer Dim sHeader As String, sFooter As String 'Open the document Set oApp = New Word.Application Set oDoc = oApp.Documents.Open(sFile) iTotalPages = oDoc.ComputeStatistics(wdStatisticPages)

'Retrieve the headers and footers on each page With oDoc iSection = 0 For iPage = 1 To iTotalPages 'Go to the page represented by the page number iPage and 'retrieve its section Set oPageStart = oDoc.GoTo(What:=wdGoToPage, _                                      Which:=wdGoToAbsolute, Count:=iPage) Set oSec = oPageStart.Sections(1) 'If this is a different section than the one in the previous 'iteration and it has a first page header/.footer, then 'retrieve the first page header/footer for this section. 'Otherwise, retrieve the primary header/footer for this section If (iSection < oSec.Index) And _ (oSec.PageSetup.DifferentFirstPageHeaderFooter) Then sHeader = oSec.Headers(wdHeaderFooterFirstPage).Range.Text sFooter = oSec.Footers(wdHeaderFooterFirstPage).Range.Text Else sHeader = oSec.Headers(wdHeaderFooterPrimary).Range.Text sFooter = oSec.Footers(wdHeaderFooterPrimary).Range.Text End If           iSection = oSec.Index 'Display the results in the debug window Debug.Print &quot;Page &quot; & iPage & &quot;, Section &quot; & iSection & _ &quot;:&quot; & vbCrLf Debug.Print &quot;  Header: &quot; & sHeader Debug.Print &quot;  Footer: &quot; & sFooter Next End With 'Make Word visible to compare the document with the results in the 'debug window oApp.Visible = True End Sub </li> Press the F5 key to run the program.</li> Click Create Document on the form, and note that the Visual Basic client starts Word and creates the new document. When this process is finished, the document is visible. Examine the document and note the varying headers and footers throughout the document.</li> Close the new document, and then quit Word.</li> Click Retrieve Headers/Footers on the form. The Visual Basic client starts Word and retrieves the header/footer text for each page in the document. The results appear in the debug window; compare these results to the document.</li></ol>

<div class="references_section">