【问题标题】:Setting Range to start on the second page of a document设置范围从文档的第二页开始
【发布时间】:2016-07-30 19:15:58
【问题描述】:

我需要更改目录的范围,以便我可以从 Word 文档的第二页开始。谁能帮我设置范围?

下面的代码是我目前拥有的范围,但这会在word文档的最开始生成目录,我需要在第二页插入。

object start = oWord.ActiveDocument.Content.Start;
Word.Range rangeForTOC = oDoc.Range(ref oMissing, ref start);

这是我正在测试的:

object gotoPage1 = Word.WdGoToItem.wdGoToPage;
object gotoNext1 = Word.WdGoToDirection.wdGoToAbsolute;
object gotoCount1 = null;
object gotoName1 = 1;

oWord.Selection.GoTo(ref gotoPage1, ref gotoNext1, ref gotoCount1, ref gotoName1);

//Insert a blank page  
oWord.Selection.InsertNewPage();
oWord.Selection.InsertNewPage();

object what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
object which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToAbsolute;
object count = 2; //change this number to specify the start of a different page

oWord.Selection.GoTo(ref what, ref which, ref count, ref oMissing);


Object beginPageTwo = oWord.Selection.Range.Start;
// This gets the start of the page specified by count object

Word.Range rangeForTOC = oDoc.Range(ref oMissing, ref beginPageTwo);

object oTrueValue = true;

【问题讨论】:

  • 尝试在第一页末尾插入分页符。

标签: c# .net ms-word office-interop


【解决方案1】:

您应该可以这样做:

object missing = System.Reflection.Missing.Value;

object what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
object which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToAbsolute;
object count = 2; //change this number to specify the start of a different page

oWord.Selection.GoTo(ref what, ref which, ref count, ref missing);
Object beginPageTwo = oWord.Selection.Range.Start; // This gets the start of the page specified by count object

Word.Range rangeForTOC = oDoc.Range(ref beginPageTwo); //modified this line per comments

以上代码包含来自SO - how can we open a word file with specific page number in c sharp?的代码

用于验证这是否基于 cmets 工作的测试代码:(已编辑)

object fileName = (object)@"C:\test.docx";
object oMissing = System.Reflection.Missing.Value;

Microsoft.Office.Interop.Word.Application oWord = new Application();
oWord.Documents.Open(ref fileName);

object gotoPage1 = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
object gotoNext1 = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToAbsolute;
object gotoCount1 = null;
object gotoName1 = 1;

oWord.Selection.GoTo(ref gotoPage1, ref gotoNext1, ref gotoCount1, ref gotoName1);

//Insert a blank page  
oWord.Selection.InsertNewPage();

object what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
object which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToAbsolute;
object count = 2; //change this number to specify the start of a different page

oWord.Selection.GoTo(ref what, ref which, ref count, ref oMissing);

Object beginPageTwo = oWord.Selection.Range.Start; // This gets the start of the page specified by count object

Microsoft.Office.Interop.Word.Range rangeForTOC = oWord.ActiveDocument.Range(ref beginPageTwo);

oWord.ActiveDocument.TablesOfContents.Add(rangeForTOC);

我使用面向 .NET Framework 4.0 的 Visual Studio 2012 Premium 针对 Word 2010 测试了此代码。

【讨论】:

  • 您可能需要将变量名称与您的特定代码相匹配,仅供参考。
  • 我真的很清楚为什么,但这仍然在第一页生成.... :( @jordanhill123
  • 如果你运行我用来验证它是否正常工作的示例测试代码,它对你有用吗?
  • 是的,如果我只是插入一个文本,它就可以工作,但是当我输入目录时,它只是跳转到第一页。检查主要问题,我添加了我正在测试的代码。
  • 好的,我想在第一页放一个段落,然后用分页符结束页面。在第二页上,我想要目录。如果您能告诉我如何在第一页插入段落,然后分页符转到第二页并输入目录,我将不胜感激。我已经尝试了大约 3 天.. :( (我认为它被称为分节符(下一页)),但是当我插入它时,它是在段落之前插入分节符,而不是在它之后,我希望分节符再次插入第一页底部),非常感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-08-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多