ControlCollection.AddPlainTextContentControl Method (Range, String)

Adds a new PlainTextContentControl at the specified range in the document.

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

Syntax

'Declaration
Function AddPlainTextContentControl ( _
    range As Range, _
    name As String _
) As PlainTextContentControl
PlainTextContentControl AddPlainTextContentControl(
    Range range,
    string name
)

Parameters

Return Value

Type: Microsoft.Office.Tools.Word.PlainTextContentControl
The PlainTextContentControl that was added to the document.

Exceptions

Exception Condition
ArgumentNullException

name is nulla null reference (Nothing in Visual Basic) or has zero length.

ControlNameAlreadyExistsException

A control with the same name is already in the ControlCollection.

Remarks

Use this method to add a new PlainTextContentControl at a specified range in the document at run time. For more information, see Adding Controls to Office Documents at Run Time.

Examples

The following code example adds a new PlainTextContentControl 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 AddTextControlAtRange method from the ThisDocument_Startup method.

Dim plainTextControl2 As Microsoft.Office.Tools.Word.PlainTextContentControl

Private Sub AddPlainTextControlAtRange()
    Me.Paragraphs(1).Range.InsertParagraphBefore()
    plainTextControl2 = Me.Controls.AddPlainTextContentControl(Me.Paragraphs(1).Range, "plainTextControl2")
    plainTextControl2.PlaceholderText = "Enter your first name"
End Sub
private Microsoft.Office.Tools.Word.PlainTextContentControl textControl2;

private void AddTextControlAtRange()
{
    this.Paragraphs[1].Range.InsertParagraphBefore();

    textControl2 = this.Controls.AddPlainTextContentControl(this.Paragraphs[1].Range,
        "textControl2");
    textControl2.PlaceholderText = "Enter your first name";
}

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

Dim plainTextControl2 As Microsoft.Office.Tools.Word.PlainTextContentControl

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

    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    vstoDoc.Paragraphs(1).Range.InsertParagraphBefore()
    plainTextControl2 = vstoDoc.Controls.AddPlainTextContentControl( _
        vstoDoc.Paragraphs(1).Range, "plainTextControl2")
    plainTextControl2.PlaceholderText = "Enter your first name"
End Sub
private Microsoft.Office.Tools.Word.PlainTextContentControl textControl2;

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

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

    textControl2 = vstoDoc.Controls.AddPlainTextContentControl(
        vstoDoc.Paragraphs[1].Range,
        "textControl2");
    textControl2.PlaceholderText = "Enter your first name";
}

.NET Framework Security

See Also

Reference

ControlCollection Interface

AddPlainTextContentControl Overload

Microsoft.Office.Tools.Word Namespace

Other Resources

Adding Controls to Office Documents at Run Time

Helper Methods for Host Controls

How to: Add Content Controls to Word Documents