C# 将范围设置为从文档的第二页开始

C# 将范围设置为从文档的第二页开始,c#,.net,ms-word,office-interop,C#,.net,Ms Word,Office Interop,我需要更改目录的范围,以便可以从word文档的第二页开始。有人能帮我设置范围吗 下面的代码是我目前拥有的范围,但这将在word文档最需要的时候生成目录,我需要在第二页插入它 object start = oWord.ActiveDocument.Content.Start; Word.Range rangeForTOC = oDoc.Range(ref oMissing, ref start); 这就是我正在测试的: object gotoPage1 = Word.WdGoToItem.wdG

我需要更改目录的范围,以便可以从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;

以下是您应该如何做到这一点:

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
上面的代码包含来自

用于验证此功能是否正常的测试代码基于注释:(已编辑)


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

您可能需要将变量名与特定代码匹配,仅供参考。我确实注意到了原因,但这仍然在第一页生成了….:(@jordanhill123如果你运行我用来验证它是否有效的示例测试代码,它对你有效吗?是的,如果我只是插入一个文本,它就有效了,但是当我放入目录时,它只是跳转到第一页。检查主要问题,我添加了我正在测试的代码。好的,我正在尝试在第一页上放一个段落,然后是一个page break以结束页面。在第二页,我想要目录。如果您能告诉我如何在第一页插入段落,然后分页符转到第二页并输入目录,我将不胜感激。我已经尝试了大约3天:((我想它被称为分节符(下一页)),但当我插入它时,它是在段落之前插入分页符,而不是在段落之后,我希望在第一页底部插入分页符)再次感谢您的帮助!尝试在第一页末尾插入分页符。
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);