Visual Studio Tools for the Microsoft Office System
How to: Insert Text into Word Documents

NoteNote

Some code examples in this topic use the this or Me keyword or the Globals class in a way that is specific to document-level customizations, or they rely on features of document-level customizations such as host controls. These examples can be compiled only if you have the required applications installed. For more information, see Features Available by Product Combination.

There are three primary ways to insert text in Microsoft Office Word 2003 documents:

Inserting Text in a Range

Use the Text property of a Range object to insert text in a document.

To insert text in a range

  1. Specify a range that is the insertion point at the beginning of a document and inserts the text New Text (note the spaces) at the insertion point.

    Visual Basic
    Dim rng As Word.Range = Me.Range(Start:=0, End:=0)
    rng.Text = " New Text "
    C#
    object start = 0; 
    object end = 0; 
    
    Word.Range rng = this.Range(ref start, ref end); 
    rng.Text = "New Text"; 
  2. Select the Range object, which now includes the inserted text.

    Visual Basic
    rng.Select()
    C#
    rng.Select();

Replacing Text in a Range

If your range is not an insertion point, all text in the range is replaced with the inserted text.

To replace text in a range

  1. Create a Range object that consists of the first 12 characters in the document.

    Visual Basic
    Dim rng As Word.Range = Me.Range(Start:=0, End:=12)
    C#
    object start = 0; 
    object end = 12; 
    
    Word.Range rng = this.Range(ref start, ref end); 
  2. Replace those characters with the string " New Text "

    Visual Basic
    rng.Text = " New Text "
    C#
    rng.Text = "New Text"; 
  3. Select the range.

    Visual Basic
    rng.Select()
    C#
    rng.Select();

Inserting Text Using TypeText

The TypeText method inserts text at the selection. TypeText behaves differently depending on the options set on the user's computer. The code in the following procedure declares a Selection object variable, and turns off the Overtype option if it is turned on. If the Overtype option is activated, then any text next to the insertion point is overwritten.

To insert text using the TypeText method

  1. Declare a Selection object variable.

    Visual Basic
    Dim currentSelection As Word.Selection = Application.Selection
    C#
    Word.Selection currentSelection = Application.Selection; 
  2. Turn off the Overtype option if it is turned on.

    Visual Basic
    If Application.Options.Overtype Then
        Application.Options.Overtype = False
    End If
    C#
    if (Application.Options.Overtype) 
    { 
        Application.Options.Overtype = false; 
    } 
  3. Test to see if the current selection is an insertion point.

    If it is, the code inserts a sentence using TypeText, and then a paragraph mark using the TypeParagraph method.

    Visual Basic
    With currentSelection
    
        ' Test to see if selection is an insertion point.
        If .Type = Word.WdSelectionType.wdSelectionIP Then
            .TypeText("Inserting at insertion point. ")
            .TypeParagraph()
    C#
    // Test to see if selection is an insertion point.
    if (currentSelection.Type == Word.WdSelectionType.wdSelectionIP) 
    { 
        currentSelection.TypeText("Inserting at insertion point. ");
        currentSelection.TypeParagraph(); 
    } 
  4. The code in the ElseIf block tests to see if the selection is a normal selection. If it is, then another If block tests to see if the ReplaceSelection option is turned on. If it is, the code uses the Collapse method of the selection to collapse the selection to an insertion point at the start of the selected block of text. Insert the text and a paragraph mark.

    Visual Basic
    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()
    C#
    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. If the selection is not an insertion point or a block of selected text, then the code in the Else block does nothing.

    Visual Basic
    Else
        ' Do nothing.
    End If
    C#
    else
    {
        // Do nothing.
    }

You can also use the TypeBackspace method of the Selection object, which mimics the functionality of the BACKSPACE key on your keyboard. However, when it comes to inserting and manipulating text, the Range object offers you more control.

The entire method that uses TypeText looks like this:

Visual Basic
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
C#
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;
}

See Also

Tags :


Page view tracker