sharpiee
sharpiee

Reputation: 55

Setting Range to start on the second page of a document

I need to change the Range of a table of contents so that i can start on the second page of a word document. Anyone can help me with setting the range?

The code below is the range that i currently have, but this will generate the table of contents at the very begging of the word document, I need to insert it on the second page.

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

This is what I'm testing this with:

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;

Upvotes: 1

Views: 6602

Answers (1)

jordanhill123
jordanhill123

Reputation: 4192

Here's how you should be able to do this:

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

Above code incorporates code from SO - how can we open a word file with specific page number in c sharp?

Test code used to verify this is working based on comments: (Edited)

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);

I tested this code against Word 2010 using Visual Studio 2012 Premium targeting .NET Framework 4.0.

Upvotes: 3

Related Questions