none
How to insert a cover page in MS Word at the beginning of the document? RRS feed

  • Question

  • I need to insert a cover page into a Word 2016 document.
    The building block is a cover page and has

        InsertOptions = (int)WdDocPartInsertOptions.wdInsertPage; //= 2

    So far so good.

    But VSTO can only insert so:

        buildingBlock.Insert(range);

    It does insert in place of range.

    The event `Application.ActiveDocument.BuildingBlockInsert` is not raised.

    Using native insert cover page (tab insert --> cover page) does insert properly (and creates only one undo entry insert building block).     

    // -----------------------------------------------------------------
    // try 1
    var range = Application.ActiveDocument.Range();
    range.Collapse(WdCollapseDirection.wdCollapseStart);
    buildingBlock.Insert(range);
    // result: inserting on the existing first page
    //         one undo entry
    //         event BuildingBlockInsert has not been raised
    // -----------------------------------------------------------------
    // try 2
    //object start = 0;
    //object end = 0;
    //var range = Application.ActiveDocument.Range(ref start, ref end);
    //buildingBlock.Insert(range);
    // result: inserting on the existing first page
    //         one undo entry
    //         event BuildingBlockInsert has not been raised
    // -----------------------------------------------------------------
    // try 3
    //var range = Application.ActiveDocument.Range();
    //range.InsertParagraphBefore();
    //var p = Application.ActiveDocument.Paragraphs[1];
    //buildingBlock.Insert(p.Range);
    // result: inserting on the existing first page
    //         two undo entries
    //         event BuildingBlockInsert has not been raised
    // -----------------------------------------------------------------


    A similar problem is described in Note of:
    https://docs.microsoft.com/en-us/office/vba/word/concepts/working-with-word/working-with-building-blocks#inserting-a-building-block-into-a-document

    It seems VSTO ignores any insert options,
    and there is no way to parameterize insert option by inserting.

    How can VSTO insert a building block in a new first page as a Word native action?

    I'm using VS 2017 Word 2016 Add-In, .Net Framework 4.6.1.




    • Edited by ''AG'' Monday, December 10, 2018 2:03 PM
    Monday, December 10, 2018 1:51 PM

Answers

All replies

  • Hi AG,

    I noticed that you also asked question on Stackoverflow. Have you tried Cindy's advice?

    The following links are some similar issues:

    How to insert/fetch a cover page in word document using Microsoft.Office.Interop.Word C#

    Word Interop C#: Insert new page using existing page

    Hopefully it helps you.

    Best Regards,

    Lina


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread.

    Tuesday, December 11, 2018 2:22 AM
  • Hi Lina,

    yes i have tried to use

    range.Collapse(WdCollapseDirection.wdCollapseStart); // = range.Collapse(); 

    but it doesn't help.

    Thanks for links.

    I am forced to use a workaround:

    range.InsertBreak(WdBreakType.wdPageBreak);

    at the document beginning;
    but it is not alike native Word cover page inserting.

    I need alike Word native inserting behavior from VSTO.

    A user must be able to combine native inserting/removing/replacement with custom(VSTO) actions without any noticeable differences.


    • Edited by ''AG'' Tuesday, December 11, 2018 11:15 AM
    Tuesday, December 11, 2018 10:38 AM
  • The discussion is here: Stackoverflow

    This thread can be closed.

    • Marked as answer by ''AG'' Wednesday, December 12, 2018 1:05 PM
    Wednesday, December 12, 2018 1:05 PM