none
How would I add a second page to a word doc and use a template RRS feed

  • Question

  • I have it so I can create and populate the first page but now I'm getting requests to add a second page (sometimes) and it would need to have the same template.

    I've tried creating a loop so after it goes through all the objects on the first page it looks to see if I need page two and if so I used:
    wordApp.Selection.InsertNewPage();  but that is putting the blank page on top.  I'm sure I'm missing something basic but I work on office apps about once every year or so, so I don't have that much exp in doing it.

    Thanks in advance!


    This is in C# BTW
    • Moved by CoolDadTx Friday, February 12, 2016 1:47 PM Office related
    • Edited by temlehdrol Friday, February 12, 2016 1:50 PM Added language due to move
    Friday, February 12, 2016 1:28 PM

Answers

  • Hi, temlehdrol

    You could refer to C# code below:

    static void DemoInsertPage(string fileName)
    {
        object missing=System.Reflection.Missing.Value;
        Word.Application wordApp = new Word.Application();
        Word.Document document = wordApp.Documents.Open(fileName);
        Word.Selection selection = wordApp.Selection;
        selection.GoTo(Word.WdGoToItem.wdGoToPage,missing,missing,"1");
        wordApp.ActiveDocument.Bookmarks[@"\Page"].Range.Copy();
        //Go forward one page
        selection.GoTo(Word.WdGoToItem.wdGoToPage,missing,missing,"2");
        //Paste copied page
        selection.Paste();
        document.Save();
        ((Word._Application)wordApp).Quit();
        document = null;
        wordApp = null;
    }

    • Proposed as answer by David_JunFeng Thursday, February 18, 2016 9:10 AM
    • Marked as answer by David_JunFeng Tuesday, February 23, 2016 7:15 AM
    Tuesday, February 16, 2016 1:36 AM

All replies

  • >>>I have it so I can create and populate the first page but now I'm getting requests to add a second page (sometimes) and it would need to have the same template.

    According to your description, if I don't misunderstand, I suggest that you could refer to below VBA code then change them into C#:

    Sub DemoCopyPage()
    
        Selection.GoTo What:=wdGoToPage, Name:="1"
        'Copy current page
        ActiveDocument.Bookmarks("\Page").Range.Copy
        'Go forward one page
        Selection.GoTo What:=wdGoToPage, Name:="2"
        'Paste copied page
        Selection.Paste
    
    End Sub
    

    For more information, click here to refer about Selection.GoTo Method (Word)

    Monday, February 15, 2016 3:20 AM
  • Looks promising... I haven't done VBA in a few years but I'm pretty sure I can modify that to give it a shot. Thanks!
    Monday, February 15, 2016 12:41 PM
  • Hi, temlehdrol

    You could refer to C# code below:

    static void DemoInsertPage(string fileName)
    {
        object missing=System.Reflection.Missing.Value;
        Word.Application wordApp = new Word.Application();
        Word.Document document = wordApp.Documents.Open(fileName);
        Word.Selection selection = wordApp.Selection;
        selection.GoTo(Word.WdGoToItem.wdGoToPage,missing,missing,"1");
        wordApp.ActiveDocument.Bookmarks[@"\Page"].Range.Copy();
        //Go forward one page
        selection.GoTo(Word.WdGoToItem.wdGoToPage,missing,missing,"2");
        //Paste copied page
        selection.Paste();
        document.Save();
        ((Word._Application)wordApp).Quit();
        document = null;
        wordApp = null;
    }

    • Proposed as answer by David_JunFeng Thursday, February 18, 2016 9:10 AM
    • Marked as answer by David_JunFeng Tuesday, February 23, 2016 7:15 AM
    Tuesday, February 16, 2016 1:36 AM