1

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;
Deduplicator
  • 44,692
  • 7
  • 66
  • 118
sharpiee
  • 55
  • 4
  • 11

1 Answers1

3

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.

Community
  • 1
  • 1
jordanhill123
  • 4,142
  • 2
  • 31
  • 40
  • You may need to match the variable names to your specific code just FYI. – jordanhill123 Sep 25 '14 at 01:24
  • I am really note sure why, but this generated on the first page still.... :( @jordanhill123 – sharpiee Sep 25 '14 at 02:10
  • If you run the sample test code that I used to verify that it is workfing, does it work for you? – jordanhill123 Sep 25 '14 at 13:59
  • Yes, it works if i just insert a text, but when i put table of contents, it just jumps to the first page. check the main question, i added the code that i'm testing this with. – sharpiee Sep 26 '14 at 00:02
  • Ok, I am trying to put a paragraph on the first page,then a page break to end the page. on the second page, i want to have the table of contents. I would really appreciate it if you could tell me how to insert the paragraph on the first page, then page break to go to the second and input the table of contents. I have been trying for about 3 days.. :( (I Think it is called section break (next page)), but when i insert it, it is inserting the break before the paragraph, not after it, i want the break to be inserted at the bottom of the first page) again, thank you so much for helping! – sharpiee Sep 26 '14 at 00:28
  • Attached test code that performs what you described in comments – jordanhill123 Sep 26 '14 at 00:48
  • Thanks. What you put there makes the table of contents go to the second page if i just use the code you provided. but when i add it to my original document, which has 15 pages, it erases most of the document. Is there a way to make it page break after the table of contents is over so that all of the text slides and not get erased? Also, how would i add paragraphs on the first page? when i try, it just doesn't appear. Thanks for your help! – sharpiee Sep 26 '14 at 01:08
  • If this solved your specific issue, I would mark this as the accepted answer and post the additional issue you are having as another question and I'll keep an eye out for it. Link back to this question as well when posting it. The code you have in this question does not reproduce the issue you just mentioned. – jordanhill123 Sep 26 '14 at 01:15
  • Hey man, this is the new thread. http://stackoverflow.com/questions/26050636/adding-paragraphs-on-the-first-page-before-tableofcontents-c-word. – sharpiee Sep 26 '14 at 01:48