如何:向 Word 文档添加内容控件

在文档级 Word 项目中,您可以在设计时或在运行时向项目中的文档添加内容控件。在应用程序级 Word 项目中,可以在运行时向任何打开的文档中添加内容控件。

**适用于:**本主题中的信息适用于 Word 2013 和 Word 2010 的文档级项目和应用程序级项目。有关更多信息,请参见按 Office 应用程序和项目类型提供的功能

本主题介绍了以下任务:

  • 在设计时添加内容控件

  • 在运行时在文档级项目中添加内容控件

  • 在运行时在应用程序级项目中添加内容控件

有关内容控件的信息,请参见内容控件

在设计时添加内容控件

可通过以下几种方法在设计时将内容控件添加到文档级项目中的文档中:

说明说明

对于在以下说明中使用的某些 Visual Studio 用户界面元素,您的计算机可能会显示不同的名称或位置。这些元素取决于您所使用的 Visual Studio 版本和您所使用的设置。有关更多信息,请参见 Visual Studio 设置

使用“工具箱”将内容控件添加到文档中

  1. 在由 Visual Studio 设计器承载的文档中,将光标放在要添加内容控件的位置,或选择想让内容控件替换的文本。

  2. 打开**“工具箱”,然后单击“Word 控件”**选项卡。

  3. 使用以下方法之一添加控件:

    • 双击**“工具箱”**中的内容控件。

    • 单击**“工具箱”**中的内容控件,然后按 Enter 键。

    • 将内容控件从**“工具箱”**拖到文档中。将在文档中当前选定内容处添加该内容控件,而不是在鼠标指针位置添加内容控件。

说明说明

使用“工具箱”无法添加 GroupContentControl。只能在 Word 中或在运行时添加 GroupContentControl

说明说明

Visual Studio 为 Word 2013 工具箱中未提供一个复选框内容控件并不 word 2010 项目。若要向文档中添加复选框内容控件,必须以编程方式创建一个 ContentControl 对象。有关更多信息,请参见内容控件

在 Word 中将内容控件添加到文档

  1. 在由 Visual Studio 设计器承载的文档中,将光标放在要添加内容控件的位置,或选择想让内容控件替换的文本。

  2. 在功能区上,单击**“开发人员”**选项卡。

    说明说明

    如果看不到“开发人员”选项卡,您必须首先显示该选项卡。有关更多信息,请参见如何:在功能区上显示“开发人员”选项卡

  3. 在**“控件”**组中单击希望添加的内容控件的图标。

在运行时在文档级项目中添加内容控件

通过在项目中使用 ThisDocument 类的 Controls 属性的方法,可以在运行时以编程方式将内容控件添加到文档中。每个方法都有三个重载,可以按下列方法使用这些重载来添加内容控件:

  • 在当前选定内容处添加控件。

  • 在指定范围处添加控件。

  • 添加一个基于文档中的本机内容控件的控件。

当关闭该文档时,不会在文档中保留动态创建内容控件。但是,本机内容控件会保留在文档中。下次打开该文档时,您可以重新创建一个基于本机内容控件的内容控件。有关更多信息,请参见在运行时向 Office 文档添加控件

说明说明

若要在 Word 2010 项目中向文档添加复选框内容控件,必须创建一个 ContentControl 对象。有关更多信息,请参见内容控件

在当前选定内容处添加内容控件

  • 使用名称为 Add<控件类>(其中控件类是希望添加的内容控件的类名,如 AddRichTextContentControl)且具有单一新控件名称参数的 ControlCollection 方法。

    下面的代码示例使用 ControlCollection.AddRichTextContentControl(String) 方法将一个新的 RichTextContentControl 添加到文档的开头。若要运行此代码,请将代码添加到项目的 ThisDocument 类中,并从 ThisDocument_Startup 事件处理程序中调用 AddRichTextControlAtSelection 方法。

    Dim richTextControl1 As Microsoft.Office.Tools.Word.RichTextContentControl
    
    Private Sub AddRichTextControlAtSelection()
        Me.Paragraphs(1).Range.InsertParagraphBefore()
        Me.Paragraphs(1).Range.Select()
        richTextControl1 = Me.Controls.AddRichTextContentControl("richTextControl1")
        richTextControl1.PlaceholderText = "Enter your first name"
    End Sub
    
    private Microsoft.Office.Tools.Word.RichTextContentControl richTextControl1;
    
    private void AddRichTextControlAtSelection()
    {
        this.Paragraphs[1].Range.InsertParagraphBefore();
        this.Paragraphs[1].Range.Select();
    
        richTextControl1 = this.Controls.AddRichTextContentControl("richTextControl1");
        richTextControl1.PlaceholderText = "Enter your first name";
    }
    

在指定的范围处添加内容控件

  • 使用名称为 Add<控件类>(其中控件类 是希望添加的内容控件类的名称,如 AddRichTextContentControl),且具有 Microsoft.Office.Interop.Word.Range 参数的 ControlCollection 方法。

    下面的代码示例使用 ControlCollection.AddRichTextContentControl(Range, String) 方法将一个新的 RichTextContentControl 添加到文档的开头。若要运行此代码,请将代码添加到项目的 ThisDocument 类中,并从 ThisDocument_Startup 事件处理程序中调用 AddRichTextControlAtRange 方法。

    Dim richTextControl2 As Microsoft.Office.Tools.Word.RichTextContentControl
    
    Private Sub AddRichTextControlAtRange()
        Me.Paragraphs(1).Range.InsertParagraphBefore()
        richTextControl2 = Me.Controls.AddRichTextContentControl(Me.Paragraphs(1).Range, _
            "richTextControl2")
        richTextControl2.PlaceholderText = "Enter your first name"
    End Sub
    
    private Microsoft.Office.Tools.Word.RichTextContentControl richTextControl2;
    
    private void AddRichTextControlAtRange()
    {
        this.Paragraphs[1].Range.InsertParagraphBefore();
    
        richTextControl2 = this.Controls.AddRichTextContentControl(this.Paragraphs[1].Range,
            "richTextControl2");
        richTextControl2.PlaceholderText = "Enter your first name";
    }
    

添加一个基于本机内容控件的内容控件

  • 使用名称为 Add<控件类>(其中控件类是希望添加的内容控件类的名称,如 AddRichTextContentControl)且具有 Microsoft.Office.Interop.Word.ContentControl 参数的 ControlCollection 方法。

    下面的代码示例使用 ControlCollection.AddRichTextContentControl(ContentControl, String) 方法来为文档中的每一个本机多格式文本控件创建一个新的 RichTextContentControl。若要运行此代码,请将代码添加到项目的 ThisDocument 类中,并从 ThisDocument_Startup 事件处理程序中调用 CreateRichTextControlsFromNativeControls 方法。

    Private richTextControls As New System.Collections.Generic.List _
            (Of Microsoft.Office.Tools.Word.RichTextContentControl)
    
    Private Sub CreateRichTextControlsFromNativeControls()
        If Me.ContentControls.Count <= 0 Then
            Return
        End If
    
        Dim count As Integer = 0
        For Each nativeControl As Word.ContentControl In Me.ContentControls
            If nativeControl.Type = Word.WdContentControlType.wdContentControlRichText Then
                count += 1
                Dim tempControl As Microsoft.Office.Tools.Word.RichTextContentControl = _
                    Me.Controls.AddRichTextContentControl(nativeControl, _
                    "VSTORichTextContentControl" + count.ToString())
                richTextControls.Add(tempControl)
            End If
        Next nativeControl
    End Sub
    
    private System.Collections.Generic.List
        <Microsoft.Office.Tools.Word.RichTextContentControl> richTextControls;
    
    private void CreateRichTextControlsFromNativeControls()
    {
        if (this.ContentControls.Count <= 0)
            return;
    
        richTextControls = new System.Collections.Generic.List
            <Microsoft.Office.Tools.Word.RichTextContentControl>();
        int count = 0;
    
        foreach (Word.ContentControl nativeControl in this.ContentControls)
        {
            if (nativeControl.Type ==
                Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlRichText)
            {
                count++;
                Microsoft.Office.Tools.Word.RichTextContentControl tempControl =
                    this.Controls.AddRichTextContentControl(nativeControl,
                    "VSTORichTextControl" + count.ToString());
                richTextControls.Add(tempControl);
            }
        }
    }
    

在运行时在应用程序级项目中添加内容控件

您可以使用应用程序级外接程序以编程方式在运行时向任何打开的文档添加内容控件。为此,请生成一个基于打开的文档的 Document 宿主项,然后使用此宿主项的 Controls 属性的方法。每个方法都有三个重载,可以按下列方法使用这些重载来添加内容控件:

  • 在当前选定内容处添加控件。

  • 在指定范围处添加控件。

  • 添加一个基于文档中的本机内容控件的控件。

当关闭该文档时,不会在文档中保留动态创建内容控件。但是,本机内容控件会保留在文档中。下次打开该文档时,您可以重新创建一个基于本机内容控件的内容控件。有关更多信息,请参见在 Office 文档中保存动态控件

有关在应用程序级项目中生成宿主项的更多信息,请参见在运行时在应用程序级外接程序中扩展 Word 文档和 Excel 工作簿

说明说明

若要添加复选框内容控件绑定到 Word 2013 的文档或 word 2010 项目,则必须创建 ContentControl 对象。有关更多信息,请参见内容控件

在当前选定内容处添加内容控件

  • 使用名称为 Add<控件类>(其中控件类是希望添加的内容控件的类名,如 AddRichTextContentControl)且具有单一新控件名称参数的 ControlCollection 方法。

    下面的代码示例使用 ControlCollection.AddRichTextContentControl(String) 方法将一个新的 RichTextContentControl 添加到活动文档的开头。若要运行此代码,请将代码添加到项目的 ThisAddIn 类中,并从 ThisAddIn_Startup 事件处理程序中调用 AddRichTextControlAtSelection 方法。

    Dim richTextControl1 As Microsoft.Office.Tools.Word.RichTextContentControl
    
    Private Sub AddRichTextControlAtSelection()
        Dim currentDocument As Word.Document = Me.Application.ActiveDocument
        currentDocument.Paragraphs(1).Range.InsertParagraphBefore()
        currentDocument.Paragraphs(1).Range.Select()
    
        Dim extendedDocument As Document = Globals.Factory.GetVstoObject(currentDocument)
    
    
        richTextControl1 = extendedDocument.Controls.AddRichTextContentControl("richTextControl1")
        richTextControl1.PlaceholderText = "Enter your first name"
    End Sub
    
    private Microsoft.Office.Tools.Word.RichTextContentControl richTextControl1;
    
    private void AddRichTextControlAtSelection()
    {
        Word.Document currentDocument = this.Application.ActiveDocument;
        currentDocument.Paragraphs[1].Range.InsertParagraphBefore();
        currentDocument.Paragraphs[1].Range.Select();
    
        Document extendedDocument = Globals.Factory.GetVstoObject(currentDocument);
    
        richTextControl1 = extendedDocument.Controls.AddRichTextContentControl("richTextControl1");
        richTextControl1.PlaceholderText = "Enter your first name";
    }
    

在指定的范围处添加内容控件

  • 使用名称为 Add<控件类>(其中控件类 是希望添加的内容控件类的名称,如 AddRichTextContentControl),且具有 Microsoft.Office.Interop.Word.Range 参数的 ControlCollection 方法。

    下面的代码示例使用 ControlCollection.AddRichTextContentControl(Range, String) 方法将一个新的 RichTextContentControl 添加到活动文档的开头。若要运行此代码,请将代码添加到项目的 ThisAddIn 类中,并从 ThisAddIn_Startup 事件处理程序中调用 AddRichTextControlAtRange 方法。

    Dim richTextControl2 As Microsoft.Office.Tools.Word.RichTextContentControl
    
    Private Sub AddRichTextControlAtRange()
        Dim currentDocument As Word.Document = Me.Application.ActiveDocument
        currentDocument.Paragraphs(1).Range.InsertParagraphBefore()
    
        Dim extendedDocument As Document = Globals.Factory.GetVstoObject(currentDocument)
    
        richTextControl2 = extendedDocument.Controls.AddRichTextContentControl( _
            extendedDocument.Paragraphs(1).Range, "richTextControl2")
        richTextControl2.PlaceholderText = "Enter your first name"
    End Sub
    
    private Microsoft.Office.Tools.Word.RichTextContentControl richTextControl2;
    
    private void AddRichTextControlAtRange()
    {
        Word.Document currentDocument = this.Application.ActiveDocument;
        currentDocument.Paragraphs[1].Range.InsertParagraphBefore();
    
        Document extendedDocument = Globals.Factory.GetVstoObject(currentDocument);
    
        richTextControl2 = extendedDocument.Controls.AddRichTextContentControl(
            currentDocument.Paragraphs[1].Range, "richTextControl2");
        richTextControl2.PlaceholderText = "Enter your first name";
    }
    

添加一个基于本机内容控件的内容控件

  • 使用名称为 Add<控件类>(其中控件类是希望添加的内容控件类的名称,如 AddRichTextContentControl)且具有 Microsoft.Office.Interop.Word.ContentControl 参数的 ControlCollection 方法。

    下面的代码示例在文档打开后,使用 ControlCollection.AddRichTextContentControl(ContentControl, String) 方法为该文档中的每一个本机多格式文本控件创建一个新的 RichTextContentControl。若要运行此代码,请将此代码添加到项目中的 ThisAddIn 类中。

    Private richTextControls As New System.Collections.Generic.List _
        (Of Microsoft.Office.Tools.Word.RichTextContentControl)
    
    Private Sub Application_DocumentOpen(ByVal Doc As Microsoft.Office.Interop.Word.Document) _
        Handles Application.DocumentOpen
    
        If Doc.ContentControls.Count > 0 Then
    
            Dim extendedDocument As Document = Globals.Factory.GetVstoObject(Doc)
    
            Dim count As Integer = 0
            For Each nativeControl As Word.ContentControl In Doc.ContentControls
                If nativeControl.Type = Word.WdContentControlType.wdContentControlRichText Then
                    count += 1
                    Dim tempControl As Microsoft.Office.Tools.Word.RichTextContentControl = _
                        extendedDocument.Controls.AddRichTextContentControl(nativeControl, _
                        "VSTORichTextContentControl" + count.ToString())
                    richTextControls.Add(tempControl)
                End If
            Next nativeControl
        End If
    End Sub
    
    private System.Collections.Generic.List
        <Microsoft.Office.Tools.Word.RichTextContentControl> richTextControls;
    
    private void Application_DocumentOpen(Microsoft.Office.Interop.Word.Document Doc)
    {
        if (Doc.ContentControls.Count > 0)
        {
            Document extendedDocument = Globals.Factory.GetVstoObject(Doc);
    
            richTextControls = new System.Collections.Generic.List
                <Microsoft.Office.Tools.Word.RichTextContentControl>();
            int count = 0;
    
            foreach (Word.ContentControl nativeControl in Doc.ContentControls)
            {
                if (nativeControl.Type ==
                    Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlRichText)
                {
                    count++;
                    Microsoft.Office.Tools.Word.RichTextContentControl tempControl =
                        extendedDocument.Controls.AddRichTextContentControl(nativeControl,
                        "VSTORichTextControl" + count.ToString());
                    richTextControls.Add(tempControl);
                }
            }
        }
    }
    

    对于 C#,还必须将 Application_DocumentOpen 事件处理程序附加到 DocumentOpen 事件。

    this.Application.DocumentOpen +=
        new Word.ApplicationEvents4_DocumentOpenEventHandler(Application_DocumentOpen);
    

请参见

概念

使用扩展对象实现 Word 自动化

宿主项和宿主控件概述

在运行时向 Office 文档添加控件

宿主项和宿主控件的编程限制

其他资源

应用程序级外接程序编程

对文档级自定义项进行编程