【问题标题】:VSTO format word-footer to be left-boundVSTO 格式 word-footer 为左边界
【发布时间】:2013-02-18 06:15:08
【问题描述】:

我需要实现一种方法,可以一键将页脚添加到由一行组成的 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 页之外的所有页面都会被删除。

没想到这么简单的任务会这么难完成。

【问题讨论】:

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


    【解决方案1】:

    另一种使用样式的方法

    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";
    

    【讨论】:

    • 主要问题仍然存在:如何将此字段添加到路径已经存在的同一行?只需添加这一行,就会用页码覆盖我的路径。
    • 您的意思是要在编写新页脚之前清理现有页脚?
    • 我已经编辑了我的原始帖子,以显示我面临的问题。
    • 当你添加 PageNumberRange.Fields.Add(PageNumberRange, Word.WdFieldType.wdFieldEmpty ,"PAGE Arabic ", true) 你删除所有页面,试试上面编辑的替代方法是否适合你
    猜你喜欢
    • 2022-01-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-19
    • 2023-04-07
    • 1970-01-01
    • 2015-03-17
    • 2020-12-17
    相关资源
    最近更新 更多