ControlCollection.AddRichTextContentControl Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
AddRichTextContentControl(String) |
Adds a new RichTextContentControl at the current selection in the document. |
AddRichTextContentControl(ContentControl, String) |
Adds a new RichTextContentControl that is based on a native content control in the document. |
AddRichTextContentControl(Range, String) |
Adds a new RichTextContentControl at the specified range in the document. |
AddRichTextContentControl(String)
Adds a new RichTextContentControl at the current selection in the document.
public:
Microsoft::Office::Tools::Word::RichTextContentControl ^ AddRichTextContentControl(System::String ^ name);
public Microsoft.Office.Tools.Word.RichTextContentControl AddRichTextContentControl (string name);
abstract member AddRichTextContentControl : string -> Microsoft.Office.Tools.Word.RichTextContentControl
Public Function AddRichTextContentControl (name As String) As RichTextContentControl
Parameters
- name
- String
The name of the new control.
Returns
The RichTextContentControl 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 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.
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";
}
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
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 AddRichTextControlAtSelection
method from the ThisAddIn_Startup
method.
private Microsoft.Office.Tools.Word.RichTextContentControl richTextControl1;
private void AddRichTextControlAtSelection()
{
if (this.Application.ActiveDocument == null)
return;
Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
vstoDoc.Paragraphs[1].Range.InsertParagraphBefore();
vstoDoc.Paragraphs[1].Range.Select();
richTextControl1 = vstoDoc.Controls.AddRichTextContentControl("richTextControl1");
richTextControl1.PlaceholderText = "Enter your first name";
}
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 = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
vstoDoc.Paragraphs(1).Range.InsertParagraphBefore()
vstoDoc.Paragraphs(1).Range.Select()
richTextControl1 = vstoDoc.Controls.AddRichTextContentControl("richTextControl1")
richTextControl1.PlaceholderText = "Enter your first name"
End Sub
Remarks
Use this method to add a new RichTextContentControl at the current selection in the document at run time. For more information, see Adding Controls to Office Documents at Run Time.
Applies to
AddRichTextContentControl(ContentControl, String)
Adds a new RichTextContentControl that is based on a native content control in the document.
public:
Microsoft::Office::Tools::Word::RichTextContentControl ^ AddRichTextContentControl(Microsoft::Office::Interop::Word::ContentControl ^ contentControl, System::String ^ name);
public Microsoft.Office.Tools.Word.RichTextContentControl AddRichTextContentControl (Microsoft.Office.Interop.Word.ContentControl contentControl, string name);
abstract member AddRichTextContentControl : Microsoft.Office.Interop.Word.ContentControl * string -> Microsoft.Office.Tools.Word.RichTextContentControl
Public Function AddRichTextContentControl (contentControl As ContentControl, name As String) As RichTextContentControl
Parameters
- contentControl
- ContentControl
The ContentControl that is the basis for the new control.
- name
- String
The name of the new control.
Returns
The RichTextContentControl 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 Microsoft.Office.Interop.Word.ContentControl.Type property of contentControl
does not have the value Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlRichText).
Examples
The following code example creates a new RichTextContentControl for every native rich 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 CreateRichTextControlsFromNativeControls
method from the ThisDocument_Startup
method.
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);
}
}
}
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
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 CreateRichTextControlsFromNativeControls
method from the ThisAddIn_Startup
method.
private System.Collections.Generic.List
<Microsoft.Office.Tools.Word.RichTextContentControl> richTextControls;
private void CreateRichTextControlsFromNativeControls()
{
if (this.Application.ActiveDocument == null)
return;
Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
if (vstoDoc.ContentControls.Count <= 0)
return;
richTextControls = new System.Collections.Generic.List
<Microsoft.Office.Tools.Word.RichTextContentControl>();
int count = 0;
foreach (Word.ContentControl nativeControl in vstoDoc.ContentControls)
{
if (nativeControl.Type ==
Microsoft.Office.Interop.Word.WdContentControlType.wdContentControlRichText)
{
count++;
Microsoft.Office.Tools.Word.RichTextContentControl tempControl =
vstoDoc.Controls.AddRichTextContentControl(nativeControl,
"VSTORichTextControl" + count.ToString());
richTextControls.Add(tempControl);
}
}
}
Private richTextControls As New System.Collections.Generic.List _
(Of Microsoft.Office.Tools.Word.RichTextContentControl)
Private Sub CreateRichTextControlsFromNativeControls()
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.wdContentControlRichText Then
count += 1
Dim tempControl As Microsoft.Office.Tools.Word.RichTextContentControl = _
vstoDoc.Controls.AddRichTextContentControl(nativeControl, _
"VSTORichTextContentControl" + count.ToString())
richTextControls.Add(tempControl)
End If
Next nativeControl
End Sub
The following code example creates a new RichTextContentControl for every native rich 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_RichTextContentControlAfterAdd
event handler to the ContentControlAfterAdd event of the ThisDocument
class.
void ThisDocument_RichTextContentControlAfterAdd(Word.ContentControl NewContentControl, bool InUndoRedo)
{
if (NewContentControl.Type == Word.WdContentControlType.wdContentControlRichText)
{
this.Controls.AddRichTextContentControl(NewContentControl,
"RichTextControl" + NewContentControl.ID);
}
}
Private Sub ThisDocument_RichTextContentControlAfterAdd(ByVal NewContentControl As Word.ContentControl, _
ByVal InUndoRedo As Boolean) Handles Me.ContentControlAfterAdd
If NewContentControl.Type = Word.WdContentControlType.wdContentControlRichText Then
Me.Controls.AddRichTextContentControl(NewContentControl, _
"RichTextControl" + 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_RichTextContentControlAfterAdd
event handler to the ContentControlAfterAdd event of the active document.
void ActiveDocument_RichTextContentControlAfterAdd(
Word.ContentControl NewContentControl, bool InUndoRedo)
{
Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
if (NewContentControl.Type == Word.WdContentControlType.wdContentControlRichText)
{
vstoDoc.Controls.AddRichTextContentControl(NewContentControl,
"RichTextControl" + NewContentControl.ID);
}
}
Private Sub ActiveDocument_RichTextContentControlAfterAdd( _
ByVal NewContentControl As Word.ContentControl, _
ByVal InUndoRedo As Boolean)
Dim vstoDoc As Document = Globals.Factory.GetVstoObject(Me.Application.ActiveDocument)
If NewContentControl.Type = Word.WdContentControlType. _
wdContentControlRichText Then
vstoDoc.Controls.AddRichTextContentControl(NewContentControl, _
"RichTextControl" + NewContentControl.ID)
End If
End Sub
Remarks
Use this method to add a new RichTextContentControl that is based on a native content control in the document at run time. This is useful when you create a RichTextContentControl 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
AddRichTextContentControl(Range, String)
Adds a new RichTextContentControl at the specified range in the document.
public:
Microsoft::Office::Tools::Word::RichTextContentControl ^ AddRichTextContentControl(Microsoft::Office::Interop::Word::Range ^ range, System::String ^ name);
public Microsoft.Office.Tools.Word.RichTextContentControl AddRichTextContentControl (Microsoft.Office.Interop.Word.Range range, string name);
abstract member AddRichTextContentControl : Microsoft.Office.Interop.Word.Range * string -> Microsoft.Office.Tools.Word.RichTextContentControl
Public Function AddRichTextContentControl (range As Range, name As String) As RichTextContentControl
Parameters
- name
- String
The name of the new control.
Returns
The RichTextContentControl 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 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 AddRichTextControlAtRange
method from the ThisDocument_Startup
method.
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";
}
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
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 AddRichTextControlAtRange
method from the ThisAddIn_Startup
method.
private Microsoft.Office.Tools.Word.RichTextContentControl richTextControl2;
private void AddRichTextControlAtRange()
{
if (this.Application.ActiveDocument == null)
return;
Document vstoDoc = Globals.Factory.GetVstoObject(this.Application.ActiveDocument);
vstoDoc.Paragraphs[1].Range.InsertParagraphBefore();
richTextControl2 = vstoDoc.Controls.AddRichTextContentControl(vstoDoc.Paragraphs[1].Range,
"richTextControl2");
richTextControl2.PlaceholderText = "Enter your first name";
}
Dim richTextControl2 As Microsoft.Office.Tools.Word.RichTextContentControl
Private Sub AddRichTextControlAtRange()
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()
richTextControl2 = vstoDoc.Controls.AddRichTextContentControl( _
vstoDoc.Paragraphs(1).Range, _
"richTextControl2")
richTextControl2.PlaceholderText = "Enter your first name"
End Sub
Remarks
Use this method to add a new RichTextContentControl at a specified range in the document at run time. For more information, see Adding Controls to Office Documents at Run Time.