【发布时间】: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 页之外的所有页面都会被删除。
没想到这么简单的任务会这么难完成。
【问题讨论】: