C# 在文本框中插入Word文档页码字段

C# 在文本框中插入Word文档页码字段,c#,interop,page-numbering,C#,Interop,Page Numbering,我在Word文档的页脚中有一个文本框。它有一些左对齐的模板文本,然后已经右对齐了“第x页,共n页”。当我尝试替换模板文本时,所有文本都被替换 使用C#或VB(无论哪种方式),我都需要替换文本框内的文本(所有文本应左对齐),然后添加“第x页,共n页”(右对齐) 以下是我目前为止的测试: string footer; foreach (Shape shape in oMyDoc.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimar

我在Word文档的页脚中有一个文本框。它有一些左对齐的模板文本,然后已经右对齐了“第x页,共n页”。当我尝试替换模板文本时,所有文本都被替换

使用C#或VB(无论哪种方式),我都需要替换文本框内的文本(所有文本应左对齐),然后添加“第x页,共n页”(右对齐)

以下是我目前为止的测试:

string footer;

foreach (Shape shape in oMyDoc.Sections[1].Footers[WdHeaderFooterIndex.wdHeaderFooterPrimary].Shapes)
{
  footer = shape.TextFrame.ContainingRange.Text;
  footer = footer.Replace("\r", "");
  footer = footer.Replace("[Quote Type]", "Big Quote");
  footer = footer.Replace("(", "\u2022");

  int start = footer.IndexOf("Pager ");

  footer = footer.Remove(start + 5);

  var numPages = oMyDoc.ComputeStatistics(WdStatistic.wdStatisticPages);

  footer = footer + " of " + numPages.ToString();

  shape.TextFrame.ContainingRange.Text = footer;
}

什么不起作用?错误?作为一个建议:看看单词Footer是否有像PowerPoint那样的“InsertPageNumber”方法。那么你就不必重新发明轮子了。@ChristianSauer不是我的代码不起作用,而是我无法找出代码来得到我描述的东西。不,没有办法只插入页码。自动页码对象是一个字段对象,我能得到的最接近的结果是用单个对象替换整个页脚。