您的位置:首页 > 编程语言 > C#

C# 操作Word知识汇总 二

2011-07-01 12:52 489 查看
删除页眉横线

http://club.excelhome.net/viewthread.php?tid=450171&highlight=%C9%BE%B3%FD

//删除内容

oDoc.Sections[1].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Cut();

//删除页眉横线 oDoc.Sections[1].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Borders[Word.WdBorderType.wdBorderBottom].LineStyle = Word.WdLineStyle.wdLineStyleNone;

该文主要涉及节的页码问题,及如何使某一节的页码从1开始,而不是按照整个文档的页码。

在Word 2003中,我们实现这种效果的步骤是这样的:

(1)插入分节符

(2)打开页眉页脚视图,取消“链接到上一页”

循此思路,我们也需在程序中作类似操作。

(1)插入分节符

Word.Section mySec = oDoc.Sections.Add(ref missing,ref missing);

(2)插入页眉页脚

(3)取消“链接到上一页”

(4)插入页码

public void setSections(int secNum) { //针对具体实例的(第三节),如需用需修改
//oDoc.Sections[secNum].PageSetup.DifferentFirstPageHeaderFooter = -1; //节的第一页与节的其它页不同(true)
//oDoc.Sections[secNum].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range.Text = "第一页的页眉";
//oDoc.Sections[secNum].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "其它页的页眉";
//oDoc.Sections[secNum].Footers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range.Text = "第一页的页脚";
//oDoc.Sections[secNum].Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "其它页的页脚";
//oDoc.Sections[secNum].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].LinkToPrevious = false; //取消链接到前一页
oDoc.Sections[secNum].Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].LinkToPrevious = false;   // 页眉
oDoc.Sections[secNum].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].LinkToPrevious = false;   // 页脚
oDoc.Sections[secNum].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "页眉内容";
//oDoc.Sections[secNum].Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "页脚内容";

oWord.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageFooter;//转到页脚视图
//oWord.Selection.Paragraphs.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter; //整个页脚居中
object page = Microsoft.Office.Interop.Word.WdFieldType.wdFieldPage; //当前页码
oWord.Selection.HeaderFooter.PageNumbers.RestartNumberingAtSection = true; // 重设起始页码
oWord.Selection.HeaderFooter.PageNumbers.StartingNumber = 1; //设为1
oWord.Selection.Fields.Add(oWord.Selection.Range, ref page, ref missing, ref missing); // 加入 页码 字段
object alignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter; //页码居中,注意必须在加入页码以后才可以用,否则出现异常
oWord.Selection.HeaderFooter.PageNumbers[1].Alignment = Microsoft.Office.Interop.Word.WdPageNumberAlignment.wdAlignPageNumberCenter;
oWord.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument; //转到正文
}
public void setSections()
{
////恢复页码编排,否则下面的节也会重新开始
oWord.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekCurrentPageFooter;//转到页脚视图
oWord.Selection.HeaderFooter.PageNumbers.RestartNumberingAtSection = false; // 重设起始页码
oWord.ActiveWindow.View.SeekView = Microsoft.Office.Interop.Word.WdSeekView.wdSeekMainDocument; //转到正文
}


几个接口或类

1. HeaderFooter
接口

LinkToPrevious 链接到前一节

PageNumbers 某header或footer中所有的page number(页码)字段的集合。

Range 某对象的文本内容,这里指页眉或页脚的文本内容,可以设置或读取。

2. PageNumbers
接口

RestartNumberingAtSection True if page numbering starts at 1 again at the beginning of the specified section.

ShowFirstPageNumber True if the page number appears on the first page in the section.

StartingNumber Returns or sets the starting note number, line number, or page number.

常见问题:



1. 如何使一节中第一页与其它也不同?

如何使 Word 自动设置和检索节页眉/页脚信息

http://support.microsoft.com/kb/269565/zh-cn

一节中是否让第一页的页眉或页脚与其他页不同是由
对象的 DifferentFirstPageHeaderFooter
属性决定的。

oDoc.Sections[3].PageSetup.DifferentFirstPageHeaderFooter = -1;

oDoc.Sections[3].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range.Text = "第一页的页眉";

oDoc.Sections[3].Headers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "其它页的页眉";

oDoc.Sections[3].Footers[Word.WdHeaderFooterIndex.wdHeaderFooterFirstPage].Range.Text = "第一页的页脚";

oDoc.Sections[3].Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range.Text = "其它页的页脚";

DifferentFirstPageHeaderFooter
属性的值是什么类型的呢?

在MSDN里,文档说它是True
if a different header or footer is used on the first page. Can be True
, False
, or wdUndefined。
但在我们C#编程中,用True却报错,在VB中是可以的。下面是一段解释:

http://social.msdn.microsoft.com/Forums/zh-CN/worddev/thread/e9f963a9-18e4-459a-a588-17824bd3906d

The value is an int in C#, and the documentation is not as helpful as it could be.

For "true", you need to use -1

For "false", you need to use 0

I do not think you can /set/ this value to wdUndefined (i.e.
Word.wdConstants.wdUndefined). However, Word may /return/ that value,
whose integer value is 9999999) if you have more than one section with
different values of DifferentFirstPageHeaderFooter,
and you test

ActiveDocument.PageSetup.DifferentFirstPageHeaderFooter

rather than the value for a particular section.

(2)Word计算统计信息,如多少页

iTotalPages = oDoc.ComputeStatistics(wdStatisticPages)

好像有个oDoc的统计属性也可以直接取得统计信息,没去测试
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: