none
How to add new page in msword document through C#.Net

    Question

  • Hi,

    I have required to insert a new page in a ms-word document programatically through C#.Net.
    I know to open or read a new document through C# but do not know to add new page in a document.

    If anyone have solution then please help me.

    Regards,
    Sandeep Soni
    Wednesday, July 16, 2008 5:38 AM

Answers

  •  

    Hi, Sandeep,

           You know how to open and read a new document via C# and your concern now is how to insert a new page through Visual C#, right?   Correct me if I misunderstand your meaning. J

           To insert a blank page or a page break at some specific page, we could use the following codes: (We could add WORD reference into the project at first)

                object missing = System.Reflection.Missing.Value;  
                object fileName = @"C:\test.docx";  
                object readOnly = false;  
                object isVisible = true;  
     
                //Start Word and open a document.  
                Word._Application oWord;  
                Word._Document oDoc;  
                oWord = new Word.Application();  
                oWord.Visible = true;  
     
                oDoc = oWord.Documents.Open(ref fileName, ref missing, ref readOnly,  
                    ref missing, ref missing, ref missing, ref missing, ref missing,  
                    ref missing, ref missing, ref missing, ref isVisible, ref missing,  
                    ref missing, ref missing, ref missing);  
     
                //Goto some specific page and insert a blank page or page break  
                object gotoPage = Word.WdGoToItem.wdGoToPage;  
                object gotoNext = Word.WdGoToDirection.wdGoToNext;  
                object gotoCount = null;  
                object gotoName = "2";  
     
                oWord.Selection.GoTo(ref gotoPage, ref gotoNext, ref gotoCount, ref gotoName);  
                  
                //Insert a blank page  
                oWord.Selection.InsertNewPage();  
     
                //Insert a page break  
                object breakPage = Word.WdBreakType.wdPageBreak;  
                oWord.Selection.InsertBreak(ref breakPage); 

           
          
           For other question on Word automation via C#, we could refer to 
           http://support.microsoft.com/kb/316384/en-us
           http://www.c-sharpcorner.com/UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx

           Any more questions, please be free to post again.

     

          Thanks,

     

    Best Regards,

    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Friday, July 18, 2008 8:31 AM
    Moderator

All replies

  •  

    Hi, Sandeep,

           You know how to open and read a new document via C# and your concern now is how to insert a new page through Visual C#, right?   Correct me if I misunderstand your meaning. J

           To insert a blank page or a page break at some specific page, we could use the following codes: (We could add WORD reference into the project at first)

                object missing = System.Reflection.Missing.Value;  
                object fileName = @"C:\test.docx";  
                object readOnly = false;  
                object isVisible = true;  
     
                //Start Word and open a document.  
                Word._Application oWord;  
                Word._Document oDoc;  
                oWord = new Word.Application();  
                oWord.Visible = true;  
     
                oDoc = oWord.Documents.Open(ref fileName, ref missing, ref readOnly,  
                    ref missing, ref missing, ref missing, ref missing, ref missing,  
                    ref missing, ref missing, ref missing, ref isVisible, ref missing,  
                    ref missing, ref missing, ref missing);  
     
                //Goto some specific page and insert a blank page or page break  
                object gotoPage = Word.WdGoToItem.wdGoToPage;  
                object gotoNext = Word.WdGoToDirection.wdGoToNext;  
                object gotoCount = null;  
                object gotoName = "2";  
     
                oWord.Selection.GoTo(ref gotoPage, ref gotoNext, ref gotoCount, ref gotoName);  
                  
                //Insert a blank page  
                oWord.Selection.InsertNewPage();  
     
                //Insert a page break  
                object breakPage = Word.WdBreakType.wdPageBreak;  
                oWord.Selection.InsertBreak(ref breakPage); 

           
          
           For other question on Word automation via C#, we could refer to 
           http://support.microsoft.com/kb/316384/en-us
           http://www.c-sharpcorner.com/UploadFile/amrish_deep/WordAutomation05102007223934PM/WordAutomation.aspx

           Any more questions, please be free to post again.

     

          Thanks,

     

    Best Regards,

    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Friday, July 18, 2008 8:31 AM
    Moderator
  • how to delete page from word using c#???
    Friday, August 08, 2008 5:33 AM
  •  

    Hi, oxygen357,

          If you have another new problem, please create a new thread to ask.  I think it will let more community members see you want some help, and both of us will be glad to help you.

          Besides, for Office development in Visual Studio, I think there are more suitable forums like: Visual Studio Tools for Office Forum

          Thanks,

     

    Best Regards,

    Lingzhi


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Friday, August 08, 2008 5:54 AM
    Moderator
  • //Insert a blank page             
     oWord.Selection.InsertNewPage();
    givng an error. 
    InsertNewPage not a function int which version you find that?

    PK
    Thursday, May 21, 2009 10:15 AM
  • Hello

          I have same problem.
          I have tried but in that there is no function for InsertNewPage();

          It gives me error on
          //Insert a blank page    
          oWord.Selection.InsertNewPage();


    Please give me reply

    Thanks & Regards Jignesh Patel
    Thursday, May 21, 2009 10:22 AM
  • Hello

             I am using Micosoft Word 11.0 Object Library(8.3 version).

            What wrong with me.

    Please give me reply.
    Thanks & Regards Jignesh Patel
    Thursday, May 21, 2009 10:34 AM