Share via


向 Word 文档添加内容控件

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

适用于: 本主题中的信息适用于 Word 的文档级项目和 VSTO 外接程序项目。 有关详细信息,请参阅办公室应用程序和项目类型提供的功能。

本主题介绍了以下任务:

在设计时添加内容控件

在设计时向文档级项目中的文档添加内容控件有以下几种方式:

  • 从“ 工具箱”“Word 控件”选项卡添加内容控件。

  • 用和在 Word 中添加本机内容控件相同的方式向文档添加内容控件。

  • “数据源” 窗口将内容控件拖动到你的文档中。 当你想要在创建控件后将控件绑定到数据时会非常有用。 有关详细信息,请参阅 How to: Populate documents with data from objects and How to: Populate documents with a data from a database.

    注意

    以下说明中的某些 Visual Studio 用户界面元素在计算机上出现的名称或位置可能会不同。 这些元素取决于你所使用的 Visual Studio 版本和你所使用的设置。 有关详细信息,请参阅个性化设置 IDE

若要使用工具箱向文档添加内容控件

  1. 在 Visual Studio 设计器中托管的文档中,将光标置于要添加内容控件的位置,或选择要替换内容控件的文本。

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

  3. 通过以下方式之一添加控件:

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

      or

    • 在工具箱单击内容控件,然后按 Enter 键。

      or

    • 将某个内容控件从 “工具箱” 拖动到文档中。 内容控件将添加到文档中当前所选内容的位置,而不是鼠标指针的位置。

注意

你不能使用 GroupContentControl “工具箱” 添加。 在 Word 中或在运行时,只可添加 GroupContentControl

注意

Visual Studio 在工具箱中不提供复选框内容控件。 若要向文档添加复选框内容控件,则必须以编程方式创建一个 ContentControl 对象。 有关详细信息,请参阅 内容控件

若要在 Word 中向文档添加内容控件

  1. 在 Visual Studio 设计器中托管的文档中,将光标置于要添加内容控件的位置,或选择要替换内容控件的文本。

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

    注意

    如果看不到 “开发人员” 选项卡,则必须首先显示它。 有关详细信息,请参阅 “如何:在功能区上显示开发人员”选项卡。

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

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

可以通过使用项目中 Controls 类的 ThisDocument 属性的方法在运行时以编程方式向文档添加内容控件。 每种方法有三个重载可用于按以下方式添加内容控件:

  • 在当前所选内容中添加控件。

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

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

    文档关闭时,动态创建的内容控件将不保留在文档中。 但是,本机内容控件会保留在文档中。 在下次打开该文档时,你可以重新创建基于本机内容控件的内容控件。 有关详细信息,请参阅在运行时向办公室文档添加控件。

注意

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

在当前所选内容中添加内容控件

  1. ControlCollection使用具有名称Add<控件类的方法(其中控件类>是要添加的内容控件的类名,例如AddRichTextContentControl),并且具有新控件名称的单个参数。

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

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

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

  1. ControlCollection使用具有名称Add<控件类的方法(其中控件类>是要添加的内容控件类的名称,例如AddRichTextContentControl),并且具有参数。Range

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

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

要添加基于本机内容控件的内容控件

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

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

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

在 VSTO 外接程序项目中的运行时添加内容控件

你可以通过使用 VSTO 外接程序在运行时以编程方式向任何打开的文档添加内容控件。 若要执行此操作,生成基于打开的文档的 Document 主机项,然后使用此主机项的 Controls 属性的方法。 每种方法有三个重载可用于按以下方式添加内容控件:

  • 在当前所选内容中添加控件。

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

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

    文档关闭时,动态创建的内容控件将不保留在文档中。 但是,本机内容控件会保留在文档中。 在下次打开该文档时,你可以重新创建基于本机内容控件的内容控件。 有关详细信息,请参阅在办公室文档中保留动态控件。

    有关在 VSTO 外接程序项目中生成主机项的详细信息,请参阅 在运行时在 VSTO 外接程序中扩展 Word 文档和 Excel 工作簿。

注意

若要向文档添加复选框内容控件,则必须创建一个 ContentControl 对象。 有关详细信息,请参阅 内容控件

在当前所选内容中添加内容控件

  1. ControlCollection使用具有名称Add<控件类的方法(其中控件类>是要添加的内容控件的类名,例如AddRichTextContentControl),并且具有新控件名称的单个参数。

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

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

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

  1. ControlCollection使用具有名称Add<控件类的方法(其中控件类>是要添加的内容控件类的名称,例如AddRichTextContentControl),并且具有参数。Range

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

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

要添加基于本机内容控件的内容控件

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

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

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