将范围设置为在文档的第二页上开始

时间:2014-09-25 00:29:56

标签: 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.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;

1 个答案:

答案 0 :(得分:3)

以下是您应该如何执行此操作的方法:

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?

的代码

用于验证此功能的测试代码基于评论工作:(已编辑)

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测试了此代码。