C# VSTO将word页脚格式化为左边界

C# VSTO将word页脚格式化为左边界,c#,ms-word,vsto,ms-office,C#,Ms Word,Vsto,Ms Office,我需要实现一种方法,一次点击添加一个页脚的word文档组成的一行。 第一部分必须是文档的绝对路径,并且必须是左边界。除此之外,还必须有与右侧对齐的实际页码 这在Excel上不是问题;在那里,我可以使用左脚,中脚,右脚。 然而,在Word上没有这样的属性可访问 编辑:我发现了一个半工作的解决方案,其中有一些错误,并且没有正确设计,因为我还找不到合适的方法 Word.Document doc = Globals.ThisAddIn.Application.ActiveDocument;

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

这在Excel上不是问题;在那里,我可以使用左脚,中脚,右脚。 然而,在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页外的所有页面都将被删除


我从来没有想到实现这么简单的任务会如此困难。

使用样式的替代方法

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)时,删除所有页面,如果上面编辑的替代方法适合您,请尝试