Share via


ControlCollection.AddPlainTextContentControl Method

Definition

Overloads

AddPlainTextContentControl(String)

Adds a new PlainTextContentControl at the current selection in the document.

AddPlainTextContentControl(ContentControl, String)

Adds a new PlainTextContentControl that is based on a native content control in the document.

AddPlainTextContentControl(Range, String)

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

AddPlainTextContentControl(String)

Adds a new PlainTextContentControl at the current selection in the document.

public:
 Microsoft::Office::Tools::Word::PlainTextContentControl ^ AddPlainTextContentControl(System::String ^ name);
public Microsoft.Office.Tools.Word.PlainTextContentControl AddPlainTextContentControl (string name);
abstract member AddPlainTextContentControl : string -> Microsoft.Office.Tools.Word.PlainTextContentControl
Public Function AddPlainTextContentControl (name As String) As PlainTextContentControl

Parameters

name
String

The name of the new control.

Returns

The PlainTextContentControl that was added to the document.

Exceptions

name is null or has zero length.

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

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 AddTextControlAtSelection method from the ThisDocument_Startup method.

private Microsoft.Office.Tools.Word.PlainTextContentControl textControl1;

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

    textControl1 = this.Controls.AddPlainTextContentControl("textControl1");
    textControl1.PlaceholderText = "Enter your first name";
}
Dim plainTextControl1 As Microsoft.Office.Tools.Word.PlainTextContentControl

Private Sub AddPlainTextControlAtSelection()
    Me.Paragraphs(1).Range.InsertParagraphBefore()
    Me.Paragraphs(1).Range.Select()
    plainTextControl1 = Me.Controls.AddPlainTextContentControl("plainTextControl1")
    plainTextControl1.PlaceholderText = "Enter your first name"
End Sub

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

private Microsoft.Office.Tools.Word.PlainTextContentControl textControl1;

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

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

    textControl1 = vstoDoc.Controls.AddPlainTextContentControl("textControl1");
    textControl1.PlaceholderText = "Enter your first name";
}
Dim plainTextControl1 As Microsoft.Office.Tools.Word.PlainTextContentControl

Private Sub AddPlainTextControlAtSelection()
    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()
    vstoDoc.Paragraphs(1).Range.Select()
    plainTextControl1 = vstoDoc.Controls.AddPlainTextContentControl("plainTextControl1")
    plainTextControl1.PlaceholderText = "Enter your first name"
End Sub

Remarks

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

Applies to

AddPlainTextContentControl(ContentControl, String)

Adds a new PlainTextContentControl that is based on a native content control in the document.

public:
 Microsoft::Office::Tools::Word::PlainTextContentControl ^ AddPlainTextContentControl(Microsoft::Office::Interop::Word::ContentControl ^ contentControl, System::String ^ name);
public Microsoft.Office.Tools.Word.PlainTextContentControl AddPlainTextContentControl (Microsoft.Office.Interop.Word.ContentControl contentControl, string name);
abstract member AddPlainTextContentControl : Microsoft.Office.Interop.Word.ContentControl * string -> Microsoft.Office.Tools.Word.PlainTextContentControl
Public Function AddPlainTextContentControl (contentControl As ContentControl, name As String) As PlainTextContentControl

Parameters

contentControl
ContentControl

The ContentControl that is the basis for the new control.

name
String

The name of the new control.

Returns

The PlainTextContentControl that was added to the document.

Exceptions

contentControl is null.-or- name is null or has zero length.

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

contentControl is not a building block gallery (that is, the Type property of contentControl does not have the value Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlText).

Examples

The following code example creates a new PlainTextContentControl for every native plain text control that is in 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 CreateTextControlsFromNativeControls method from the ThisDocument_Startup method.

private System.Collections.Generic.List<Microsoft.Office.Tools.Word.PlainTextContentControl> plainTextControls;

private void CreateTextControlsFromNativeControls()
{
    if (this.ContentControls.Count <= 0)
        return;

    plainTextControls = new System.Collections.Generic.List
        <Microsoft.Office.Tools.Word.PlainTextContentControl>();
    int count = 0;

    foreach (Word.ContentControl nativeControl in this.ContentControls)
    {
        if (nativeControl.Type == Word.WdContentControlType.wdContentControlText)
        {
            count++;
            Microsoft.Office.Tools.Word.PlainTextContentControl tempControl =
                this.Controls.AddPlainTextContentControl(nativeControl,
                "VSTOPlainTextContentControl" + count.ToString());
            plainTextControls.Add(tempControl);
        }
    }
}
Private plainTextControls As New System.Collections.Generic.List _
    (Of Microsoft.Office.Tools.Word.PlainTextContentControl)

Private Sub CreatePlainTextControlsFromNativeControls()
    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.wdContentControlText Then
            count += 1
            Dim tempControl As Microsoft.Office.Tools.Word.PlainTextContentControl = _
                Me.Controls.AddPlainTextContentControl(nativeControl, _
                "VSTOPlainTextContentControl" + count.ToString())
            plainTextControls.Add(tempControl)
        End If
    Next nativeControl
End Sub

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

private System.Collections.Generic.List<Microsoft.Office.Tools.Word.PlainTextContentControl> plainTextControls;

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

    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    if (vstoDoc.ContentControls.Count <= 0)
        return;

    plainTextControls = new System.Collections.Generic.List
        <Microsoft.Office.Tools.Word.PlainTextContentControl>();
    int count = 0;

    foreach (Word.ContentControl nativeControl in vstoDoc.ContentControls)
    {
        if (nativeControl.Type == Word.WdContentControlType.wdContentControlText)
        {
            count++;
            Microsoft.Office.Tools.Word.PlainTextContentControl tempControl =
                vstoDoc.Controls.AddPlainTextContentControl(nativeControl,
                "VSTOPlainTextContentControl" + count.ToString());
            plainTextControls.Add(tempControl);
        }
    }
}
Private plainTextControls As New System.Collections.Generic.List _
    (Of Microsoft.Office.Tools.Word.PlainTextContentControl)

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

    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    If vstoDoc.ContentControls.Count <= 0 Then
        Return
    End If

    Dim count As Integer = 0
    For Each nativeControl As Word.ContentControl In vstoDoc.ContentControls
        If nativeControl.Type = Word.WdContentControlType.wdContentControlText Then
            count += 1
            Dim tempControl As Microsoft.Office.Tools.Word.PlainTextContentControl = _
                vstoDoc.Controls.AddPlainTextContentControl(nativeControl, _
                "VSTOPlainTextContentControl" + count.ToString())
            plainTextControls.Add(tempControl)
        End If
    Next nativeControl
End Sub

The following code example creates a new PlainTextContentControl for every native plain text control that the user adds to the document.

This version is for a document-level customization. To use this code, paste it into the ThisDocument class in your project. For C#, you must also attach the ThisDocument_PlainTextContentControlAfterAdd event handler to the ContentControlAfterAdd event of the ThisDocument class.

void ThisDocument_PlainTextContentControlAfterAdd(Word.ContentControl NewContentControl, bool InUndoRedo)
{
    if (NewContentControl.Type == Word.WdContentControlType.wdContentControlText)
    {
        this.Controls.AddPlainTextContentControl(NewContentControl,
            "PlainTextControl" + NewContentControl.ID);
    }
}
Private Sub ThisDocument_PlainTextContentControlAfterAdd(ByVal NewContentControl As Word.ContentControl, _
    ByVal InUndoRedo As Boolean) Handles Me.ContentControlAfterAdd

    If NewContentControl.Type = Word.WdContentControlType.wdContentControlText Then
        Me.Controls.AddPlainTextContentControl(NewContentControl, _
            "PlainTextControl" + NewContentControl.ID)
    End If
End Sub

This version is for an application-level add-in that targets the .NET Framework 4 or the .NET Framework 4.5. To use this code, paste it into the ThisAddIn class in your project. Also, you must attach the ActiveDocument_PlainTextContentControlAfterAdd event handler to the ContentControlAfterAdd event of the active document.

void ActiveDocument_PlainTextContentControlAfterAdd(
    Word.ContentControl NewContentControl, bool InUndoRedo)
{
    Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
    if (NewContentControl.Type == Word.WdContentControlType.wdContentControlText)
    {
        vstoDoc.Controls.AddPlainTextContentControl(NewContentControl,
            "PlainTextControl" + NewContentControl.ID);
    }
}
Private Sub ActiveDocument_PlainTextContentControlAfterAdd( _
    ByVal NewContentControl As Word.ContentControl, _
    ByVal InUndoRedo As Boolean)

    Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
    If NewContentControl.Type = Word.WdContentControlType. _
        wdContentControlText Then
        vstoDoc.Controls.AddPlainTextContentControl(NewContentControl, _
            "PlainTextControl" + NewContentControl.ID)
    End If
End Sub

Remarks

Use this method to add a new PlainTextContentControl that is based on a native content control in the document at run time. This is useful when you create a PlainTextContentControl at run time, and you want to recreate the same control the next time the document is opened. For more information, see Adding Controls to Office Documents at Run Time.

Applies to

AddPlainTextContentControl(Range, String)

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

public:
 Microsoft::Office::Tools::Word::PlainTextContentControl ^ AddPlainTextContentControl(Microsoft::Office::Interop::Word::Range ^ range, System::String ^ name);
public Microsoft.Office.Tools.Word.PlainTextContentControl AddPlainTextContentControl (Microsoft.Office.Interop.Word.Range range, string name);
abstract member AddPlainTextContentControl : Microsoft.Office.Interop.Word.Range * string -> Microsoft.Office.Tools.Word.PlainTextContentControl
Public Function AddPlainTextContentControl (range As Range, name As String) As PlainTextContentControl

Parameters

range
Range

A Range that provides the bounds for the new control.

name
String

The name of the new control.

Returns

The PlainTextContentControl that was added to the document.

Exceptions

name is null or has zero length.

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

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.

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

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

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

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.

Applies to