更新:2007 年 11 月
| 适用对象 | 
|---|
| 本主题中的信息仅适用于指定的 Visual Studio Tools for Office 项目和 Microsoft Office 版本。 项目类型 
 Microsoft Office 版本 
 有关更多信息,请参见按应用程序和项目类型提供的功能。 | 
可通过三种主要方式将文本插入 Microsoft Office Word 文档:
- 在范围中插入文本。 
- 将范围中的文本替换为新文本。 
- 使用 Selection 对象的 TypeText 方法可在光标位置或选定内容处插入文本。 .gif) 说明: 说明:- 也可以将文本插入内容控件和书签。有关更多信息,请参见内容控件和Bookmark 控件。 
在范围中插入文本
在范围中插入文本
- 在文档开头指定一个范围,并插入文本“新文本”。 - 下面的代码示例可用于文档级自定义项。 - Dim rng As Word.Range = Me.Range(Start:=0, End:=0) rng.Text = " New Text "- object start = 0; object end = 0; Word.Range rng = this.Range(ref start, ref end); rng.Text = "New Text";- 下面的代码示例可用于应用程序级外接程序。此代码使用活动文档。 - Dim rng As Word.Range = Me.Application.ActiveDocument.Range(Start:=0, End:=0) rng.Text = " New Text "- object start = 0; object end = 0; Word.Range rng = this.Application.ActiveDocument.Range( ref start, ref end); rng.Text = "New Text";
- 选择 Range 对象,该对象已从一个字符扩展为所插入文本的长度。 - rng.Select()- rng.Select();
在范围中替换文本
如果指定的范围包含文本,则该范围内的所有文本将替换为插入的文本。
在范围中替换文本
- 创建一个 Range 对象,该对象包含文档中的前 12 个字符。 - 下面的代码示例可用于文档级自定义项。 - Dim rng As Word.Range = Me.Range(Start:=0, End:=12)- object start = 0; object end = 12; Word.Range rng = this.Range(ref start, ref end);- 下面的代码示例可用于应用程序级外接程序。此代码使用活动文档。 - Dim rng As Word.Range = Me.Application.ActiveDocument.Range(Start:=0, End:=12)- object start = 0; object end = 12; Word.Range rng = this.Application.ActiveDocument.Range( ref start, ref end);
- 将这些字符替换为字符串“新文本”。 - rng.Text = " New Text "- rng.Text = "New Text";
- 选择范围。 - rng.Select()- rng.Select();
使用 TypeText 插入文本
TypeText 方法用于在选定区域插入文本。根据用户计算机上设置的选项,TypeText 的行为会有所不同。以下过程中的代码声明一个 Selection 对象变量,然后关闭 Overtype 选项(如果它处于打开状态)。如果激活了 Overtype 选项,则会覆盖光标旁边的任何文本。
使用 TypeText 方法插入文本
- 声明一个 Selection 对象变量。 - Dim currentSelection As Word.Selection = Application.Selection- Word.Selection currentSelection = Application.Selection;
- 如果 Overtype 选项是打开的,则将其关闭。 - If Application.Options.Overtype Then Application.Options.Overtype = False End If- if (Application.Options.Overtype) { Application.Options.Overtype = false; }
- 测试当前选定内容是否是插入点。 - 如果是,则代码会使用 TypeText 插入句子,然后使用 TypeParagraph 方法插入段落标记。 - With currentSelection ' Test to see if selection is an insertion point. If .Type = Word.WdSelectionType.wdSelectionIP Then .TypeText("Inserting at insertion point. ") .TypeParagraph()- // Test to see if selection is an insertion point. if (currentSelection.Type == Word.WdSelectionType.wdSelectionIP) { currentSelection.TypeText("Inserting at insertion point. "); currentSelection.TypeParagraph(); }
- ElseIf 块中的代码测试该选定内容是否为正常选定内容。如果是,则另一个 If 块将测试 ReplaceSelection 选项是否已启用。如果已经打开,代码将使用选择的 Collapse 方法将选定内容折叠到选定的文本块开头的插入点。插入文本和段落标记。 - ElseIf .Type = Word.WdSelectionType.wdSelectionNormal Then ' Move to start of selection. If Application.Options.ReplaceSelection Then .Collapse(Direction:=Word.WdCollapseDirection.wdCollapseStart) End If .TypeText("Inserting before a text block. ") .TypeParagraph()- else if (currentSelection.Type == Word.WdSelectionType.wdSelectionNormal) { // Move to start of selection. if (Application.Options.ReplaceSelection) { object direction = Word.WdCollapseDirection.wdCollapseStart; currentSelection.Collapse(ref direction); } currentSelection.TypeText("Inserting before a text block. "); currentSelection.TypeParagraph(); }
- 如果选定内容既不是插入点也不是选定的文本块,则 Else 块中的代码不执行任何操作。 - Else ' Do nothing. End If- else { // Do nothing. }
还可以使用 Selection 对象的 TypeBackspace 方法,该方法模拟键盘上的 Backspace 键的功能。不过,在需要插入并处理文本时,Range 对象可以为您提供更多的控制。
下面的示例演示完整的代码。若要使用此示例,请从项目内的 ThisDocument 或 ThisAddIn 类中运行代码。
Friend Sub SelectionInsertText()
    Dim currentSelection As Word.Selection = Application.Selection
    ' Store the user's current Overtype selection
    Dim userOvertype As Boolean = Application.Options.Overtype
    ' Make sure Overtype is turned off.
    If Application.Options.Overtype Then
        Application.Options.Overtype = False
    End If
    With currentSelection
        ' Test to see if selection is an insertion point.
        If .Type = Word.WdSelectionType.wdSelectionIP Then
            .TypeText("Inserting at insertion point. ")
            .TypeParagraph()
        ElseIf .Type = Word.WdSelectionType.wdSelectionNormal Then
            ' Move to start of selection.
            If Application.Options.ReplaceSelection Then
                .Collapse(Direction:=Word.WdCollapseDirection.wdCollapseStart)
            End If
            .TypeText("Inserting before a text block. ")
            .TypeParagraph()
            Else
                ' Do nothing.
            End If
    End With
    ' Restore the user's Overtype selection
    Application.Options.Overtype = userOvertype
End Sub
private void SelectionInsertText() 
{ 
    Word.Selection currentSelection = Application.Selection; 
    // Store the user's current Overtype selection
    bool userOvertype = Application.Options.Overtype;
    // Make sure Overtype is turned off.
    if (Application.Options.Overtype) 
    { 
        Application.Options.Overtype = false; 
    } 
    // Test to see if selection is an insertion point.
    if (currentSelection.Type == Word.WdSelectionType.wdSelectionIP) 
    { 
        currentSelection.TypeText("Inserting at insertion point. ");
        currentSelection.TypeParagraph(); 
    } 
    else 
        if (currentSelection.Type == Word.WdSelectionType.wdSelectionNormal)
        { 
            // Move to start of selection.
            if (Application.Options.ReplaceSelection)
            { 
                object direction = Word.WdCollapseDirection.wdCollapseStart;
                currentSelection.Collapse(ref direction);
            }
            currentSelection.TypeText("Inserting before a text block. ");
            currentSelection.TypeParagraph();
        }
        else
        {
            // Do nothing.
        }
    // Restore the user's Overtype selection
    Application.Options.Overtype = userOvertype;
}