locked
insert a page break and go a new page in a word document using C# Console Application....

    Question

  • I am trying to develop a code to insert a page break and go a new page in a word document using C# Console Application....But I am not able to do it.................So if any one can device any method explicitly it will be good on my part

    Word.Paragraph Line21;

    Object breakPage = new object();

    breakPage = Word.WdBreakType.wdPageBreak;

    oWordDoc.Characters.Last.InsertBreak(ref breakPage);

    But this proved to be of no use to me.



    Friday, August 29, 2008 1:33 PM

Answers

  •  
    Object nullobj = System.Reflection.Missing.Value;  
     
    Object objBreak = Word.WdBreakType.wdPageBreak;  
    Object objUnit = Word.WdUnits.wdStory;  
     
    word1.Selection.EndKey(ref objUnit, ref nullobj);  
    word1.Selection.InsertBreak(ref objBreak);  
     

    Don't forget to click "Mark as Answer" on the post that helped you. Anuja Ninan MCSD
    • Proposed as answer by Anuja Annie Ninan Friday, August 29, 2008 4:00 PM
    • Marked as answer by jack 321 Wednesday, September 03, 2008 2:52 AM
    Friday, August 29, 2008 3:34 PM

All replies

  • i am unable to proceed .. kindly help

    Friday, August 29, 2008 1:34 PM
  •  
    Object nullobj = System.Reflection.Missing.Value;  
     
    Object objBreak = Word.WdBreakType.wdPageBreak;  
    Object objUnit = Word.WdUnits.wdStory;  
     
    word1.Selection.EndKey(ref objUnit, ref nullobj);  
    word1.Selection.InsertBreak(ref objBreak);  
     

    Don't forget to click "Mark as Answer" on the post that helped you. Anuja Ninan MCSD
    • Proposed as answer by Anuja Annie Ninan Friday, August 29, 2008 4:00 PM
    • Marked as answer by jack 321 Wednesday, September 03, 2008 2:52 AM
    Friday, August 29, 2008 3:34 PM
  • I tried the code given by Anuja bit still it's not working for me.When I wrote something into the word document using c# and then leave the remaining space and want to go to the next page using page break this is not working.I am giving the following sequence of steps for it:

    First I take some values using stringbuilder:

     

    sbGenerateLine18.Append(" ");

    sbGenerateLine18.Append(TOTALS + "".PadRight((6 - TOTALS.Length), ' '));

    sbGenerateLine18.Append("".PadLeft((13 - sumDetailTotalCharge.ToString().Length), ' ')+ sumDetailTotalCharge);

    sbGenerateLine18.Append(" ");

    sbGenerateLine18.Append("".PadLeft((13 - sumDetailTotalRepricingAmount.ToString().Length), ' ')

    + sumDetailTotalRepricingAmount);

    sbGenerateLine18.AppendLine();


    Then I write this into the word document:

    Word.Paragraph Line18;

    Line18 = oWordDoc.Content.Paragraphs.Add(ref oMissing);

    Line18.Range.Text = sbGenerateLine18.ToString();

    Line18.Range.InsertParagraphAfter();



    Then I take a second set of stringbuilders:



    sbGenerateLine21.Append(" ");

    sbGenerateLine21.Append(TOTALS + "".PadRight((6 - TOTALS.Length), ' '));

    sbGenerateLine21.Append(" ");

    sbGenerateLine21.Append("".PadLeft((13 - SumTotCharge.ToString().Length), ' ')

    + SumTotCharge);

    sbGenerateLine21.Append("".PadLeft((13 - SumTotRepAmount.ToString().Length), ' ')

    + SumTotRepAmount);

    sbGenerateLine21.AppendLine();

    For this I want to generate the pagebreak and go to the next page which I am not able to do:

    I used this code but it didn't work:

    Object nullobj = System.Reflection.Missing.Value;

    Object objBreak = Word.WdBreakType.wdPageBreak;

    Object objUnit = Word.WdUnits.wdStory;

    oWord.Selection.EndKey(ref objUnit, ref nullobj);

    oWord.Selection.InsertBreak(ref objBreak);


     

    Word.Paragraph Line21;

    Line21 = oWordDoc.Content.Paragraphs.Add(ref oMissing);

    Line21.Range.Text = sbGenerateLine21.ToString();


    But it didnot help me in any way...................Plz tell me how to proceed.....

    Thanks
    Supriyo



     



     

    Saturday, August 30, 2008 7:07 AM
  • I have got the solution to this problem finally:
    The solution is :

    //Insert Pagebreak

    Word.Paragraph Line20;

    Line20 = oWordDoc.Content.Paragraphs.Add(ref oMissing);

    Line20.Range.Text = string.Empty;

    object oCollapseEnd = Word.WdCollapseDirection.wdCollapseEnd;

    object oPageBreak = Word.WdBreakType.wdPageBreak;

    Line20.Range.Collapse(ref oCollapseEnd);

    Line20.Range.InsertBreak(ref oPageBreak);

    Line20.Range.Collapse(ref oCollapseEnd);

    Line20.Range.InsertParagraphAfter();




    This solution will finally Insertbreak the current word page and open a new word object................Thanks for everybody's support.
    Monday, September 08, 2008 12:23 PM