如何在文档开头的MS Word中插入封面? [英] How to insert a cover page in MS Word at the beginning of the document?

查看:138
本文介绍了如何在文档开头的MS Word中插入封面?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在Word 2016文档中插入封面. 该构造块是封面,具有

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.

但是VSTO只能这样插入:

But VSTO can only insert so:

buildingBlock.Insert(range);

它确实插入了范围.

未引发事件Application.ActiveDocument.BuildingBlockInsert.

使用本机插入封面(选项卡插入->封面)确实可以正确插入(并且仅创建一个撤消条目插入构造块).

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

似乎VSTO忽略了任何插入选项, 并且没有办法通过插入来参数化插入选项.

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

VSTO如何作为Word本机操作在新的第一页中插入构件块?

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

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

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

推荐答案

摘要:

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

这篇关于如何在文档开头的MS Word中插入封面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆