VSTO格式的word-footer是左边的

时间:2013-03-04 07:45:36

标签: c# ms-word vsto ms-office

我需要实现一种方法来单击添加页脚到由一行组成的word文档。 第一部分需要是文档的绝对路径,并且必须是左边的。除此之外,还必须将实际页码与右侧对齐。

这不是Excel上的问题;我可以使用LeftFooter,CenterFooter,RightFooter。 但是在Word上没有这样的属性可供访问。

编辑:我发现了一个半工作的解决方案,其中有一些错误并且设计不当,因为我找不到合适的方法。

Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;
        foreach (Word.Section wordSection in doc.Sections)
        {
            Word.Range PageNumberRange = wordSection.Range;
            PageNumberRange.Fields.Add(PageNumberRange, Word.WdFieldType.wdFieldEmpty ,"PAGE  Arabic ", true);


            Word.Range footer = wordSection.Footers[Word.WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;
            footer.ParagraphFormat.Alignment = Word.WdParagraphAlignment.wdAlignParagraphCenter;
            footer.Tables.Add(footer, 1, 3);

            Word.Table tbl = footer.Tables[1];

            tbl.Cell(1, 1).Range.Text = doc.FullName;
            tbl.Cell(1, 3).Range.Text = PageNumberRange.Text;
            /**/

            footer.Font.ColorIndex = Word.WdColorIndex.wdBlack;
            footer.Font.Size = 6;

            PageNumberRange.Text = "";

这个问题是:它永远不会覆盖现有的页脚。如果它写入“document1 ... 1”并再次单击它,因为您保存了文档,它会更改页脚。此外:如果您有多个页面,除第1页以外的每个页面都会被删除。

我从没想过要实现这么简单的任务可能会如此艰难。

1 个答案:

答案 0 :(得分:0)

使用样式的替代方法

Document doc = this.application.ActiveDocument;
Section wordSection = doc.Sections[1];    
Range footer = wordSection.Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Range;                              
footer.Fields.Add(footer, WdFieldType.wdFieldEmpty, @"PAGE \* ARABIC", true);                               
footer.Collapse(WdCollapseDirection.wdCollapseStart);
footer.InsertBefore("\t \t");
footer.InsertBefore(doc.FullName);                                                          
footer.Font.Name = "Arial";