http://msdn.microsoft.com/zh-cn/library/ms178795.aspx
可以使用 Section 的Headers
屬性和Footers 屬性向文檔中的頁首和頁尾添加文本。文檔的每一部分都包含三個頁首和頁尾:
wdHeaderFooterPrimary
wdHeaderFooterEvenPages
wdHeaderFooterFirstPage
對於文件層級自訂項和應用程式層級增益集,這些過程有所不同。
適用於:本主題中的資訊適用於 Word 2007 和 Word 2010 的文檔級項目和應用程式級項目。有關更多資訊,請參見按 Office 應用程式和項目類型提供的功能。
文件層級自訂項
若要使用以下各程式碼範例,請從項目內的 ThisDocument 類運行這些樣本。
向文檔中的頁尾添加文本
下面的程式碼範例設定要插入到文檔各部分首頁腳中的文本的字型,然後將文本插入到頁尾中。
C#VBforeach (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";}
向文檔中的頁首添加文本
下面的程式碼範例添加一個欄位以在文檔中的每個頁首中顯示頁碼,然後設定段落對齊以使文本與頁首呈靠右對齊。
C#VBforeach (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;}
應用程式層級增益集
若要使用以下各程式碼範例,請從項目內的 ThisAddIn 類運行這些樣本。
向文檔中的頁尾添加文本
下面的程式碼範例設定要插入到文檔各部分首頁腳中的文本的字型,然後將文本插入到頁尾中。此程式碼範例使用使用中文件。
C#VBforeach (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";}
向文檔中的頁首添加文本
下面的程式碼範例添加一個欄位以在文檔中的每個頁首中顯示頁碼,然後設定段落對齊以使文本與頁首呈靠右對齊。此程式碼範例使用使用中文件。
C#VBforeach (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;}