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.
Range.Collapse
method before using the range inBuildingBlock.Insert
. Since you want it as the first page I recommend usingWord.WdCollapseDirection.wdCollapseStart
as the method argument. – Cindy Meister Dec 6 '18 at 16:57