如何在受保护的word文档中使用Word RepeatingSection ContentControl

How to use the Word RepeatingSection ContentControl in a protected word document

列表项

我正在 Visual Studio 2017 年使用 VSTO 和 c# 为表单填写应用程序实现一个 Word 模板,并希望利用 Word 重复部分内容控制。但是,在我之前保护用于表单填写的文档后,我无法以编程方式应用这种类型的控件。似乎取消保护文档不会 return 文档在这种情况下处于与保护它之前相同的未受保护状态。这是一个精简的演示程序来突出问题:

在Visual Studio新建Word 2013和2016 VSTO Template项目,保留项目使用不变的默认空白文档模板,在ThisDocument部分添加如下代码class

private void ThisDocument_Startup(object sender, System.EventArgs e)
    {
        //Demonstrates an unexpected impact of protecting then subsequently unprotecting a document 
        AddTableDirect();
        DocProtect();
        DocUnprotect();
        AddTableRepeatingSection();
    }
    private void ThisDocument_Shutdown(object sender, System.EventArgs e)
    {
    }
    private void DocProtect()
    {
        //Protects the active document restricting the operator to form filling
        object noReset = true;
        object password = System.String.Empty;
        object useIRM = false;
        object enforceStyleLock = false;
        this.Protect(Word.WdProtectionType.wdAllowOnlyFormFields,
            ref noReset, ref password, ref useIRM, ref enforceStyleLock);
    }
    private void DocUnprotect()
    {
        // Unprotects the active document allowing programmatic manipulation
        object password = System.String.Empty;
        this.Unprotect(ref password);
    }
    private void AddTableDirect()
    {
        //Creates a one row table directly adding a single plain text content control
        Word.Range range = this.Sections[1].Range.Paragraphs[1].Range;
        Word.Table table = this.Tables.Add
            (range, 1, 1, Word.WdDefaultTableBehavior.wdWord9TableBehavior, Word.WdAutoFitBehavior.wdAutoFitWindow);
        Word.ContentControl cc = this.ContentControls.Add
            (Word.WdContentControlType.wdContentControlText, table.Cell(1, 1).Range);
    }
    private void AddTableRepeatingSection()
    {
        //Programatically duplicates the table as a repeating section
        Word.Table table = this.Sections[1].Range.Tables[1];
        Word.Range rSRange = table.Range;
        Word.ContentControl rSCC = this.ContentControls.Add
            (Word.WdContentControlType.wdContentControlRepeatingSection, rSRange);
        rSCC.RepeatingSectionItems[1].InsertItemAfter();
    }

如果您按原样构建并 运行 此代码,则会生成 System.Runtime.InteropServices.COMException 文本:"This method or property is not available because the current selection partially covers a plain text content control" 在 AddTableRepeatingSection() 方法(InsertItemAfter 之前的行)。

但是,如果您注释掉 ThisDocument_StartUp 中的 DocProtect()DocUnprotect() 语句,那么此代码 运行 会成功。

我需要更改什么才能在以编程方式应用重复节内容控件时保护和取消保护文档而不生成此异常?

我可以复制您所看到的内容 - 我不知道它为什么这样做,似乎是某种竞争条件,因为在打开文档后(单击 "Continue")它可以手动工作...

我找到了解决方法。似乎选择 table 会将导致 Word 在它所属的第一个单元格中拾取内容控件的任何内容:

private void AddTableRepeatingSection()
{
    //Programatically duplicates the table as a repeating section
    Word.Table table = this.Sections[1].Range.Tables[1];
    Word.Range rSRange = table.Range;
    rSRange.Select();
    Word.Range r = this.Application.Selection.Range;
    Word.ContentControl rSCC = this.ContentControls.Add
        (Word.WdContentControlType.wdContentControlRepeatingSection, r);
    rSCC.RepeatingSectionItems[1].InsertItemAfter();
}