Author: Tomas Rutkauskas
Can anyone provide an example of how to set the header and footer for an entire
Word document, replacing any existing header or footer?
Answer:
This example assumes one section and no odd/ even or different first page headers.
Doing the Range.Select selects all text that may have been previously there, so you
can use this to write the first time or to change it later. Note that if you want
different headers on different pages (besides odd/ even) you will need to use
sections.
1 { ... }2 Word.ActiveDocument.Sections.Item(1).Headers.Item(1).Range.Select;
3 Word.Selection.ParagraphFormat.TabStops.ClearAll;
4 {Set tab types to be used in header}5 OleVar := wdAlignTabLeft;
6 OleVar2 := wdTabLeaderSpaces;
7 {Set tabs}8 Word.Selection.ParagraphFormat.TabStops.Add(190, OleVar, OleVar2);
9 Word.Selection.ParagraphFormat.TabStops.Add(365, OleVar, OleVar2);
10 {Now tab over and write the header field}11 Word.Selection.TypeText(WideString(#9)); {to centered text}12 Word.Selection.TypeText(WideString('This Is The Header'));
13 Word.Selection.TypeText(WideString(#9));
14 Word.Selection.TypeText(WideString('Blah Blah'));
15 {Now do the footer}16 Word.ActiveDocument.Sections.Item(1).Footers.Item(1).Range.Select;
17 Word.Selection.ParagraphFormat.TabStops.ClearAll;
18 {Set tab types to be used in header}19 OleVar := wdAlignTabLeft;
20 OleVar2 := wdTabLeaderSpaces;
21 {Set tabs}22 Word.Selection.ParagraphFormat.TabStops.Add(190, OleVar, OleVar2);
23 Word.Selection.TypeText(WideString(#9));
24 Word.Selection.TypeText(WideString('This Is The Footer'));
25 { ... }