Thursday, May 7, 2009

Add Header and Footer in Word document

Header:

foreach (Word.Section section in this.Application.ActiveDocument.Sections)
{
object fieldEmpty = Word.WdFieldType.wdFieldEmpty;
object autoText = "AUTOTEXT \"Page X of Y\" ";
object preserveFormatting = true;

section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Fields.Add(
section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range,
ref fieldEmpty, ref autoText, ref preserveFormatting);
section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
.Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight;
}


Footer:

Word.Document document = this.Application.ActiveDocument;
foreach (Word.Section wordSection in document.Sections)
{
wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
.Range.Font.ColorIndex = Word.WdColorIndex.wdDarkRed;

wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
.Range.Font.Size = 20;
wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
.Range.Text = "Confidential";
}

2 comments:

 
Locations of visitors to this page