Udostępnij za pośrednictwem


RichTextContentControl Class (2007 System)

Represents a block of text or other types of content in a document.

Namespace:  Microsoft.Office.Tools.Word
Assembly:  Microsoft.Office.Tools.Word.v9.0 (in Microsoft.Office.Tools.Word.v9.0.dll)

Syntax

'Declaration
<DefaultBindingPropertyAttribute("Text")> _
<PermissionSetAttribute(SecurityAction.Demand, Name := "FullTrust")> _
Public NotInheritable Class RichTextContentControl _
    Inherits ContentControlBase _
    Implements ISupportInitializeControl, ISupportInitialize
'Usage
Dim instance As RichTextContentControl
[DefaultBindingPropertyAttribute("Text")]
[PermissionSetAttribute(SecurityAction.Demand, Name = "FullTrust")]
public sealed class RichTextContentControl : ContentControlBase, 
    ISupportInitializeControl, ISupportInitialize
[DefaultBindingPropertyAttribute(L"Text")]
[PermissionSetAttribute(SecurityAction::Demand, Name = L"FullTrust")]
public ref class RichTextContentControl sealed : public ContentControlBase, 
    ISupportInitializeControl, ISupportInitialize
public final class RichTextContentControl extends ContentControlBase implements ISupportInitializeControl, ISupportInitialize

Remarks

A RichTextContentControl can contain text as well as many other types of content, including tables, pictures, or other content controls. You can apply a combination of formatting and styles to text in a RichTextContentControl.

To create a document region that can only contain text, use a PlainTextContentControl.

Content Controls

The RichTextContentControl is one of eight types of content controls that you can use to design documents and templates in Microsoft Office Word. Content controls have a user interface (UI) that has controlled input like a form. You can use content controls to prevent users from editing protected sections of the document or template, and you can also bind content controls to a data source. For more information, see Content Controls.

Examples

The following code example adds a new RichTextContentControl to the beginning of the document.

This version is for a document-level customization. To use this code, paste it into the ThisDocument class in your project, and call the AddRichTextControlAtSelection method from the ThisDocument_Startup method.

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

This version is for an application-level add-in. To use this code, paste it into the ThisAddIn class in your project, and call the AddRichTextControlAtSelection method from the ThisAddIn_Startup method.

Dim richTextControl1 As Microsoft.Office.Tools.Word.RichTextContentControl

Private Sub AddRichTextControlAtSelection()
    If Me.Application.ActiveDocument Is Nothing Then 
        Return 
    End If 

    Dim vstoDoc As Document = Me.Application.ActiveDocument.GetVstoObject()
    vstoDoc.Paragraphs(1).Range.InsertParagraphBefore()
    vstoDoc.Paragraphs(1).Range.Select()
    richTextControl1 = vstoDoc.Controls.AddRichTextContentControl("richTextControl1")
    richTextControl1.PlaceholderText = "Enter your first name" 
End Sub
private Microsoft.Office.Tools.Word.RichTextContentControl richTextControl1;

private void AddRichTextControlAtSelection()
{
    if (this.Application.ActiveDocument == null)
        return;

    Document vstoDoc = this.Application.ActiveDocument.GetVstoObject();
    vstoDoc.Paragraphs[1].Range.InsertParagraphBefore();
    vstoDoc.Paragraphs[1].Range.Select();

    richTextControl1 = vstoDoc.Controls.AddRichTextContentControl("richTextControl1");
    richTextControl1.PlaceholderText = "Enter your first name";
}

Inheritance Hierarchy

System.Object
  Microsoft.VisualStudio.Tools.Office.RemoteComponent
    Microsoft.VisualStudio.Tools.Office.RemoteBindableComponent
      Microsoft.Office.Tools.Word.ContentControlBase
        Microsoft.Office.Tools.Word.RichTextContentControl

Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

See Also

Reference

RichTextContentControl Members

Microsoft.Office.Tools.Word Namespace

Other Resources

Content Controls

How to: Add Content Controls to Word Documents

How to: Protect Parts of Documents by Using Content Controls

Walkthrough: Creating a Template By Using Content Controls

Change History

Date

History

Reason

July 2008

Added a version of the code example for an application-level add-in.

SP1 feature change.