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

更新:2007 年 11 月

适用对象

本主题中的信息仅适用于指定的 Visual Studio Tools for Office 项目和 Microsoft Office 版本。

项目类型

  • 文档级项目

  • 应用程序级项目

Microsoft Office 版本

  • Word 2007

有关更多信息,请参见按应用程序和项目类型提供的功能

在文档级项目中,您可以在设计时或在运行时向项目中的文档添加内容控件。从 Visual Studio 2008 Service Pack 1 (SP1) 开始,您还可以通过使用 Word 2007 的应用程序级外接程序在运行时向任何打开的文档添加内容控件。

本主题介绍了以下任务:

  • 在设计时添加内容控件

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

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

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

在设计时添加内容控件

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

说明:

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

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

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

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

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

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

      - 或 -

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

      - 或 -

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

说明:

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

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

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

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

    说明:

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

  3. 在“控件”组中单击想要添加的内容控件的图标。

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

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

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

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

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

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

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

  • 使用名称为 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);
            }
        }
    }
    

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

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

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

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

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

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

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

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

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

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

    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 = currentDocument.GetVstoObject()
        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 = currentDocument.GetVstoObject();
        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 事件处理程序中调用 ThisAddIn 方法。

    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 = currentDocument.GetVstoObject()
        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 = currentDocument.GetVstoObject();
        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 = Doc.GetVstoObject()
            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 = Doc.GetVstoObject();
            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);
    

请参见

概念

宿主项和宿主控件概述

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

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

应用程序级外接程序编程

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

宿主控件的帮助器方法

其他资源

Word 宿主控件

修订记录

日期

修订

原因

2008 年 7 月

增加了针对应用程序级外接程序的过程。

SP1 功能更改。