C# 操作Word知识汇总 二

来源:互联网 发布:人工智能 配方 编辑:程序博客网 时间:2024/06/08 17:50

删除页眉横线

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)插入页码

 

几个接口或类

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的统计属性也可以直接取得统计信息,没去测试
 
原创粉丝点击