更新:2007 年 11 月
按照指定的大小和位置向文档中添加一个新的 RichTextBox 控件。
命名空间:  Microsoft.Office.Tools.Word
程序集:  Microsoft.Office.Tools.Word.v9.0(在 Microsoft.Office.Tools.Word.v9.0.dll 中)
语法
声明
Public Function AddRichTextBox ( _
    range As Range, _
    width As Single, _
    height As Single, _
    name As String _
) As RichTextBox
用法
Dim instance As ControlCollection
Dim range As Range
Dim width As Single
Dim height As Single
Dim name As String
Dim returnValue As RichTextBox
returnValue = instance.AddRichTextBox(range, _
    width, height, name)
public RichTextBox AddRichTextBox(
    Range range,
    float width,
    float height,
    string name
)
参数
- range 
 类型:Microsoft.Office.Interop.Word.Range- 提供控件的位置的 Range。 
- width 
 类型:System.Single- 控件的宽度(以磅为单位)。 
- height 
 类型:System.Single- 控件的高度(以磅为单位)。 
- name 
 类型:System.String- 可用于索引 ControlCollection 实例中控件的名称。 
返回值
类型:Microsoft.Office.Tools.Word.Controls.RichTextBox
添加到 ControlCollection 实例的 RichTextBox 控件。
异常
| 异常 | 条件 | 
|---|---|
| ArgumentNullException | name 或 range 参数为 nullnull 引用(在 Visual Basic 中为 Nothing),或者 name 参数长度为零。 | 
| ControlNameAlreadyExistsException | ControlCollection 实例中已存在一个同名控件。 | 
| InvalidRangeException | 指定的范围无效。 | 
备注
此方法使您能够将 RichTextBox 对象添加到 ControlCollection 的末尾。
若要移除通过编程方式添加的 RichTextBox,请使用 Remove 方法。
示例
下面的代码示例向文档的第一个段落添加一个 RichTextBox 控件,向该控件分配文本,然后设置该文本的格式。
Private Sub WordRangeAddRichTextBox()
    Me.Paragraphs(1).Range.InsertParagraphBefore()
    Dim RichTextBox1 As Microsoft.Office.Tools.Word.Controls. _
        RichTextBox = Me.Controls.AddRichTextBox( _
        Me.Paragraphs(1).Range, 75, 15, "RichTextBox1")
    RichTextBox1.Text = "Sample text"
    RichTextBox1.Select(0, 6)
    RichTextBox1.SelectionFont = New Font("Times New Roman", _
        10, FontStyle.Bold)
End Sub 
private void WordRangeAddRichTextBox()
{
    this.Paragraphs[1].Range.InsertParagraphBefore();
    Microsoft.Office.Tools.Word.Controls.RichTextBox
         richTextBox1 = this.Controls.AddRichTextBox(
         this.Paragraphs[1].Range, 75, 15, "richTextBox1");
    richTextBox1.Text = "Sample text";
    richTextBox1.Select(0, 6);
    richTextBox1.SelectionFont = new Font("Times New Roman",
        10, FontStyle.Bold);
}
权限
- 对直接调用方的完全信任。此成员不能由部分信任的代码使用。有关更多信息,请参见通过部分受信任的代码使用库。