如何在文档开头插入封面?

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

我需要在 Word 2016 文档中插入封面。 积木是封面,有

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

到目前为止一切顺利。

但是VSTO只能这样插入:

buildingBlock.Insert(range);

它确实在范围内插入。

未引发事件 Application.ActiveDocument.BuildingBlockInsert

使用本机插入封面(制表符插入 --> 封面)确实可以正确插入(并且只创建一个撤消条目插入构建块)。

        // -----------------------------------------------------------------
        // 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
        // -----------------------------------------------------------------

注意事项中描述了类似的问题: https://docs.microsoft.com/en-us/office/vba/word/concepts/working-with-word/working-with-building-blocks#inserting-a-building-block-into-a-document

似乎 VSTO 忽略了任何插入选项, 并且无法通过插入来参数化插入选项。

VSTO 如何将构建块作为 Word 本机操作插入新的首页?

我正在使用 VS 2017 Word 2016 加载项、.Net Framework 4.6.1。

总结:

  1. VSTO 仅 "imitate" 本机 Word 构建块插入,并非所有特性 (InsertOptions) 和功能(文档事件 BuildingBlockInsert)都被绑定。
  2. 结合本机 Word Ui 用户体验(add/replace,删除封面)和您的自定义 VSTO 加载项(add/replace 封面)仅在有条件的情况下可行。

我当前的代码:

private static Microsoft.Office.Interop.Word.Application Application => Globals.ThisAddIn.Application;

private void InsertCoverPage(BuildingBlock buildingBlock)
{
    // validate not null
    if(buildingBlock == null) throw new ArgumentNullException(nameof(buildingBlock));

    // validate is a cover page
    if (buildingBlock.Type.Index != (int)WdBuildingBlockTypes.wdTypeCoverPage &&
        buildingBlock.Type.Index != (int)WdBuildingBlockTypes.wdTypeCustomCoverPage)
    {
        throw new ArgumentNullException(nameof(buildingBlock));
    }

    // validate insert option
    if (buildingBlock.InsertOptions != (int) WdDocPartInsertOptions.wdInsertPage) 
    {
        throw new Exception(
            "building block as a cover page must been inserted in a new page at the beginning of document");
    }

    Application.ScreenUpdating = false;

    Range range = GetCurrentCoverPageRange() ?? Application.ActiveDocument.Range(0, 0);// search a first existing cover page range

    range.InsertBreak(WdBreakType.wdPageBreak); // case existing cover page: replace by page break 
    // range.Start = 0; // = 0
    range.End = 0; // reset only end position
    buildingBlock.Insert(range, true);

    Marshal.ReleaseComObject(range);

    Application.ScreenUpdating = true;
}

private Range GetCurrentCoverPageRange()
{
    Range result = null;

    // Word insert natively a cover page with 2 paragraphs - so we need found these first 2 consecutive paragraphs marked as a cover page
    for (int i = 1; i < Application.ActiveDocument.Paragraphs.Count + 1; i++)
    {
        var paragraph = Application.ActiveDocument.Paragraphs[i];
        var isCoverPage = (bool)paragraph.Range.Information[WdInformation.wdInCoverPage];
        if (isCoverPage)
        {
            if (result == null)
            {
                result = paragraph.Range;
            }
            else
            {
                result.End = paragraph.Range.End;
            }
        }
        else
        {
            if (result != null)
            {
                break;
            }
        }
    }

    return result;
}