如何在文档开头的MS Word中插入封面?

时间:2018-12-06 16:46:46

标签: c# ms-word interop vsto office-interop

我需要在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 个答案:

答案 0 :(得分:0)

摘要:

  1. VSTO只是“模仿”了本机Word构建块的插入,并且未绑定所有功能(InsertOptions)和功能(文档事件BuildingBlockInsert)。
  2. 仅在有条件的情况下,才能将本机Word Ui用户体验(添加/替换,删除封面)与自定义VSTO加载项(添加/替换封面)组合在一起。

我当前的代码:

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;
}