如何在文档的开头插入在MS Word封面?(How to insert a cover page i

2019-11-05 09:02发布

我需要插入封面插入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 -嵌段成-A-文件

这似乎VSTO忽略任何插入选项,并没有办法通过插入到参数插入选项。

VSTO如何可以插入一个新的第一页积木为Word本地行动?

我使用VS 2017年的Word 2016插件,.Net框架4.6.1。

Answer 1:

摘要:

  1. VSTO只是“模仿”本机Word积木插入,而不是所有功能(InsertOptions)和功能(文档事件BuildingBlockInsert)的约束。
  2. 结合本机Word UI用户的expirience(添加/替换,删除封面页)和自定义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;
}


文章来源: How to insert a cover page in MS Word at the beginning of the document?