
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 cursor is overwritten.
To insert text using the TypeText method
Declare a Selection object variable.
Dim currentSelection As Word.Selection = Application.Selection
Word.Selection currentSelection = Application.Selection;
Turn off the Overtype option if it is turned on.
If Application.Options.Overtype Then
Application.Options.Overtype = False
End If
if (Application.Options.Overtype)
{
Application.Options.Overtype = false;
}
Test to see whether 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.
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();
}
The code in the ElseIf block tests to see whether the selection is a normal selection. If it is, then another If block tests to see whether 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.
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();
}
If the selection is not an insertion point or a block of selected text, then the code in the Else block does nothing.
Else
' Do nothing.
End If
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 following example shows the complete code. To use this example, run the code from the ThisDocument or ThisAddIn class in your project.
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;
}