none
Setting Range to start on the second page of a word document (c#) RRS feed

  • Question

  • Hello,

     

    I have code that generates a table of contents on a word document.

     

    The code that you see below will generate it on the first page of the word document.  

     

    I need help with changing the code so that it would generate on the second page of the document. 

     

    Any help is appreciated.

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

    Thank you.

    Thursday, September 25, 2014 12:34 AM

Answers

  • If your selection point is in page 1, you can use the following to goto the top of page 2.

        Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, count:=1


    Kind Regards, Rich ... http://greatcirclelearning.com

    Thursday, September 25, 2014 1:11 AM
  • Hi Sharpiee,

    To go to the second page, the value of count should be 2 and the code would be like this:

    Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=2

    More information about Selection.GoTo Method, please refer to:

    # Selection.GoTo Method (Word)

    http://msdn.microsoft.com/en-us/library/office/ff821348(v=office.15).aspx

    This is the sample of VSTO code:

    private void PageRange()
            {
                object oMission = System.Type.Missing;
                var oWord = Globals.ThisDocument.ActiveWindow;
                var oDoc = Globals.ThisDocument;
                object what = Word.WdGoToItem.wdGoToPage;
                object which = Word.WdGoToDirection.wdGoToAbsolute;
                object count = 2; //change this number to specify the start of a different page
               object range= oWord.Selection.GoTo(ref what, ref which, ref count, ref oMission);          
                object beginPageTwo = oWord.Selection.Range.Start; // This gets the start of the page specified by count object
                object end = oWord.Selection.Range.End+10;
                Word.Range rangeForTOC = oDoc.Range(ref beginPageTwo, ref end);
                
                //Set text to the second page
                var p = oDoc.Paragraphs.Add(ref range);
                p.Range.Text = "page2 paragraph";
    
                rangeForTOC.Font.Size = 12;
                rangeForTOC.Font.Name = "Arial";
                rangeForTOC.Font.Bold = 0;
            }

    Best Regards

    Starain Chen


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, September 25, 2014 10:00 AM
    Moderator

All replies

  • If your selection point is in page 1, you can use the following to goto the top of page 2.

        Selection.GoTo What:=wdGoToPage, Which:=wdGoToNext, count:=1


    Kind Regards, Rich ... http://greatcirclelearning.com

    Thursday, September 25, 2014 1:11 AM
  • Thanks for the reply.  

    I tried below, but it still generated on the first page.  Do you see a mistake in the code or you have any ideas? 

    object what = Microsoft.Office.Interop.Word.WdGoToItem.wdGoToPage;
                object which = Microsoft.Office.Interop.Word.WdGoToDirection.wdGoToAbsolute;
                object count = 3; //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;
                
                Word.TableOfContents toc = oDoc.TablesOfContents.Add(rangeForTOC, ref oTrueValue, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oMissing, ref oTrueValue);
                Word.Range rngTOC = toc.Range;
    
                rngTOC.Font.Size = 12;
                rngTOC.Font.Name = "Arial";
                rngTOC.Font.Bold = 0;

    Thursday, September 25, 2014 2:12 AM
  • Hi Sharpiee,

    To go to the second page, the value of count should be 2 and the code would be like this:

    Selection.GoTo What:=wdGoToPage, Which:=wdGoToAbsolute, Count:=2

    More information about Selection.GoTo Method, please refer to:

    # Selection.GoTo Method (Word)

    http://msdn.microsoft.com/en-us/library/office/ff821348(v=office.15).aspx

    This is the sample of VSTO code:

    private void PageRange()
            {
                object oMission = System.Type.Missing;
                var oWord = Globals.ThisDocument.ActiveWindow;
                var oDoc = Globals.ThisDocument;
                object what = Word.WdGoToItem.wdGoToPage;
                object which = Word.WdGoToDirection.wdGoToAbsolute;
                object count = 2; //change this number to specify the start of a different page
               object range= oWord.Selection.GoTo(ref what, ref which, ref count, ref oMission);          
                object beginPageTwo = oWord.Selection.Range.Start; // This gets the start of the page specified by count object
                object end = oWord.Selection.Range.End+10;
                Word.Range rangeForTOC = oDoc.Range(ref beginPageTwo, ref end);
                
                //Set text to the second page
                var p = oDoc.Paragraphs.Add(ref range);
                p.Range.Text = "page2 paragraph";
    
                rangeForTOC.Font.Size = 12;
                rangeForTOC.Font.Name = "Arial";
                rangeForTOC.Font.Bold = 0;
            }

    Best Regards

    Starain Chen


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.


    Thursday, September 25, 2014 10:00 AM
    Moderator