using Microsoft.Office.Interop.Word; //ver 12.0
/// <summary>
/// 自動頁碼,頁眉頁腳
/// 20120603 塗聚文
/// Geovin Du
/// </summary>
/// <param name="pPageHeader">頁眉自定義文字</param>
/// <param name="pPageFoot">頁腳自定義文字</param>
public void SetPageHeaderAndFooter(string pPageHeader, string pPageFoot)
{
Object missing = System.Reflection.Missing.Value;
//1
//添加页眉
//this._wordApplication.ActiveWindow.View.Type = Microsoft.Office.Interop.Word.WdViewType.wdOutlineView;
//this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekPrimaryHeader;
//this._wordApplication.ActiveWindow.ActivePane.Selection.InsertAfter(pPageHeader);
////设置中间对齐
//this._wordApplication.Selection.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphCenter;
////跳出页眉设置
//this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument;
//_wordApplication.DisplayAlerts = Microsoft.Office.Interop.Word.WdAlertLevel.wdAlertsMessageBox;
//頁腳
//this._wordApplication.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekEvenPagesFooter;
//this._wordApplication.ActiveWindow.ActivePane.Selection.InsertAfter(pPageHeader);
//http://www.c-sharpcorner.com/UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx
//2.设置要插入到文档各部分主页脚中的文本的字体。此代码示例使用活动文档。
//http://msdn.microsoft.com/zh-cn/library/ms178795(v=vs.90).aspx
foreach (Microsoft.Office.Interop.Word.Section wordSection in this._wordDocument.Sections)
{
wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdDarkRed;
wordSection.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Font.ColorIndex = Microsoft.Office.Interop.Word.WdColorIndex.wdBlue;
wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Font.Size = 9;
wordSection.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Font.Size = 9;
//2.将文本插入到页脚中
wordSection.Footers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = pPageFoot;
wordSection.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = pPageHeader;
}
//1.添加“自动图文集”项,以在文档的每个页眉中显示“第 X 页,共 Y 页
//foreach (Microsoft.Office.Interop.Word.Section section in this._wordDocument.Sections)
//{
// object fieldEmpty = Microsoft.Office.Interop.Word.WdFieldType.wdFieldEmpty;
// object autoText = "AUTOTEXT \"Page X of Y\" ";
// object preserveFormatting = true;
// section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Fields.Add(
// section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range,
// ref fieldEmpty, ref autoText, ref preserveFormatting);
// section.Headers[Microsoft.Office.Interop.Word.WdHeaderFooterIndex.wdHeaderFooterPrimary]
// .Range.ParagraphFormat.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight;
//}
//自动页码设置
//John Yang http://social.msdn.microsoft.com/Forums/en/vsto/thread/a044ff2d-b4a7-4f19-84f4-f3d5c55396a8
object oAlignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
// Open up the footer in the word document
this._wordApplication.ActiveWindow.ActivePane.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageFooter;
// Set current Paragraph Alignment to Center
this._wordApplication.ActiveWindow.ActivePane.Selection.Paragraphs.Alignment = Microsoft.Office.Interop.Word.WdParagraphAlignment.wdAlignParagraphRight; //.wdAlignParagraphCenter
// Type in 'Page '
this._wordApplication.ActiveWindow.Selection.TypeText("Page ");
// Add in current page field
Object CurrentPage = Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage;
this._wordApplication.ActiveWindow.Selection.Fields.Add(this._wordApplication.ActiveWindow.Selection.Range, ref CurrentPage, ref missing, ref missing);
// Type in ' of '
this._wordApplication.ActiveWindow.Selection.TypeText(" of ");
// Add in total page field
Object TotalPages = Microsoft.Office.Interop.Word.WdFieldType.wdFieldNumPages;
this._wordApplication.ActiveWindow.Selection.Fields.Add(this._wordApplication.ActiveWindow.Selection.Range, ref TotalPages, ref missing, ref missing);
object start = 0;
object end = 7;
Microsoft.Office.Interop.Word.Range rg = this._wordDocument.Range(ref start,ref end);
rg.Select();
}
哲学管理(学)人生, 文学艺术生活, 自动(计算机学)物理(学)工作, 生物(学)化学逆境, 历史(学)测绘(学)时间, 经济(学)数学金钱(理财), 心理(学)医学情绪, 诗词美容情感, 美学建筑(学)家园, 解构建构(分析)整合学习, 智商情商(IQ、EQ)运筹(学)成功.---Geovin Du(涂聚文)