共用方式為


HOW TO:將文字插入 Word 文件中

更新:2007 年 11 月

適用於

本主題中的資訊僅適用於指定的 Visual Studio Tools for Office 專案和 Microsoft Office 版本。

專案類型

  • 文件層級專案

  • 應用程式層級專案

Microsoft Office 版本

  • Word 2003

  • Word 2007

如需詳細資訊,請參閱依應用程式和專案類型提供的功能

在 Microsoft Office Word 文件中插入文字的方式主要有三種:

  • 在範圍中插入文字。

  • 以新文字取代範圍中的文字。

  • 使用 Selection 物件的 TypeText 方法將文字插入至游標或選取位置。

    注意事項:

    您也可以將文字插入至內容控制項與書籤中。如需詳細資訊,請參閱 內容控制項書籤控制項

在範圍中插入文字

使用 Range 物件的 Text 屬性將文字插入文件。

若要在範圍中插入文字

  1. 在文件的開頭指定範圍,並插入文字 New Text。

    下列程式碼範例可以用於文件層級自訂中。

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

    下列程式碼範例可以用於應用程式層級的增益集中。這個程式碼使用主動式文件 (Active Document)。

    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";
    
  2. 選取 Range 物件,這時物件已從一個字元擴展為所插入文字的長度。

    rng.Select()
    
    rng.Select();
    

取代範圍中的文字

如果指定的範圍包含文字,則範圍內的所有文字都會以插入的文字取代。

若要取代範圍中的文字

  1. 建立一個包含文件中前 12 個字元的 Range 物件。

    下列程式碼範例可以用於文件層級自訂中。

    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); 
    

    下列程式碼範例可以用於應用程式層級的增益集中。這個程式碼使用主動式文件 (Active Document)。

    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);
    
  2. 以 New Text 字串取代這些字元。

    rng.Text = " New Text "
    
    rng.Text = "New Text"; 
    
  3. 選取這個範圍。

    rng.Select()
    
    rng.Select();
    

使用 TypeText 插入文字

TypeText 方法會將文字插入選取範圍。TypeText 會依據使用者電腦設定的選項,而有不同的運作方式。下列程序中的程式碼宣告了 Selection 物件變數,並且關閉 Overtype 選項 (如果是開啟的)。如果啟用 Overtype 選項,則會覆寫游標後面的任何文字。

若要使用 TypeText 方法插入文字

  1. 宣告 Selection 物件變數。

    Dim currentSelection As Word.Selection = Application.Selection
    
    Word.Selection currentSelection = Application.Selection; 
    
  2. 關閉 Overtype 選項 (如果是開啟的)。

    If Application.Options.Overtype Then
        Application.Options.Overtype = False
    End If
    
    if (Application.Options.Overtype) 
    { 
        Application.Options.Overtype = false; 
    } 
    
  3. 測試目前的選取範圍是否為插入點。

    如果是,程式碼就會使用 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(); 
    } 
    
  4. 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();
        }
    
  5. 如果選取範圍不是插入點或選取文字的區塊,則 Else 區塊中的程式碼不會執行任何動作。

    Else
        ' Do nothing.
    End If
    
    else
    {
        // Do nothing.
    }
    

您也可以使用 Selection 物件的 TypeBackspace 方法,這個方法會模仿鍵盤上退格鍵 (BACKSPACE) 的功能。但是,如果要插入和處理文字,Range 物件可以讓您有更多的控制能力。

下列範例顯示完整程式碼。若要使用這個範例,請從專案中的 ThisDocument 或 ThisAddIn 類別 (Class) 中執行程式碼。

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

請參閱

工作

HOW TO:在文件中格式化文字

HOW TO:在文件中定義及選取範圍

HOW TO:擴充文件中的範圍