如何:向文档添加页眉和页脚
更新:2007 年 11 月
适用对象 |
---|
本主题中的信息仅适用于指定的 Visual Studio Tools for Office 项目和 Microsoft Office 版本。 项目类型
Microsoft Office 版本
有关更多信息,请参见按应用程序和项目类型提供的功能。 |
可以使用 Section 的 Headers 属性和 Footers 属性向文档中的页眉和页脚添加文本。文档的每一部分都包含三个页眉和页脚:
对于文档级自定义项和应用程序级外接程序,这些过程有所不同。
文档级自定义项
向文档中的页脚添加文本
设置要插入到文档各部分主页脚中的文本的字体。
Dim section As Word.Section For Each section In Me.Sections section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _ .Range.Font.ColorIndex = Word.WdColorIndex.wdDarkRed section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _ .Range.Font.Size = 20
foreach (Word.Section wordSection in this.Sections) { wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary] .Range.Font.ColorIndex = Word.WdColorIndex.wdDarkRed; wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary] .Range.Font.Size = 20;
将文本插入到页脚中。
section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _ .Range.Text = "Confidential" Next
wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary] .Range.Text = "Confidential"; }
向文档中的页眉添加文本
添加“自动图文集”项,以在文档的每个页眉中显示“第 X 页,共 Y 页”。
Dim section As Word.Section For Each section In Me.Sections section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Fields.Add( _ section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range, _ Word.WdFieldType.wdFieldEmpty, "AUTOTEXT ""Page X of Y"" ", True)
foreach (Word.Section section in this.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 Next
section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary] .Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight; }
编译代码
若要使用这些代码示例,请从项目内的 ThisDocument 类中运行这些示例。
应用程序级外接程序
向文档中的页脚添加文本
设置要插入到文档各部分主页脚中的文本的字体。此代码示例使用活动文档。
Dim section As Word.Section Dim document As Word.Document = Me.Application.ActiveDocument For Each section In document.Sections section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _ .Range.Font.ColorIndex = Word.WdColorIndex.wdDarkRed section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _ .Range.Font.Size = 20
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;
将文本插入到页脚中。
section.Footers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary) _ .Range.Text = "Confidential" Next
wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary] .Range.Text = "Confidential"; }
向文档中的页眉添加文本
添加“自动图文集”项,以在文档的每个页眉中显示“第 X 页,共 Y 页”。此代码示例使用活动文档。
Dim section As Word.Section For Each section In Me.Application.ActiveDocument.Sections section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range.Fields.Add( _ section.Headers(Word.WdHeaderFooterIndex.wdHeaderFooterPrimary).Range, _ Word.WdFieldType.wdFieldEmpty, "AUTOTEXT ""Page X of Y"" ", True)
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 Next
section.Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary] .Range.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphRight; }
编译代码
若要使用这些代码示例,请从项目内的 ThisAddIn 类中运行这些示例。