Inserting & Deleting
TypeText
Inserts the text at the beginning of the current selection.
The selection is turned into an insertion point at the end of the inserted text.
If Options.ReplaceSelection = True then the original selection will be replaced.
Selection.TypeText "some text"
This behaves exactly the same as typing some text at the keyboard.
TypeParagraph
Insert a paragraph mark at the beginning of the current selection.
The selection is turned into an insertion point after the inserted paragraph mark.
If Options.ReplaceSelection = True then the original selection will be replaced.
Selection.TypeParagraph
This behaves exactly the same as pressing the Enter key.
TypeBackspace
Insert a paragraph mark at the beginning of the current selection.
If the selection is an insertion point, delete the character in front of the insertion point.
If something is currently selected then the selection is turned into an insertion point at the beginning of the current selection.
If Options.ReplaceSelection = True then the original selection will be replaced.
Selection.TypeBackspace
InsertBefore
Inserts the specified text before the Range or Selection.
After the text is inserted, the range or selection is expanded to include the new text.
objRange.InsertBefore(Text:="hello world")
If the selection or range is a bookmark, the bookmark is also expanded to include the next text.
You can insert characters such as quotation marks, tab characters, and nonbreaking hyphens by using the Visual Basic Chr function with the InsertBefore method.
You can also use the following Visual Basic constants: vbCr, vbLf, vbCrLf and vbTab.
This example inserts the text "Solutions" (enclosed in quotation marks) before the selection and then collapses the selection.
With Selection
.InsertBefore Chr(34) & "Solutions" & Chr(34) & Chr(32)
.Collapse Direction:=wdCollapseDirection.wdCollapseEnd
End With
This example inserts the text "Introduction" as a separate paragraph at the beginning of the active document.
With ActiveDocument.Content
.InsertParagraphBefore
.InsertBefore "Introduction"
End With
This example inserts all the font names in the FontNames collection into a new document.
Documents.Add
For Each aFont In FontNames
With Selection
.InsertBefore aFont
.Collapse Direction:=wdCollapseDirection.wdCollapseEnd
.TypeParagraph
End With
Next aFont
InsertAfter
Inserts the specified text after the Range or Selection.
After the text is inserted, the range or selection expanded to include the new text.
objRange.InsertAfter(Text:="hello world")
You can insert characters such as quotation marks, tab characters, and nonbreaking hyphens by using the Visual Basic Chr function with the InsertAfter method.
If you use this method with a range or selection that refers to an entire paragraph, the text is inserted after the ending paragraph mark (the text will appear at the beginning of the next paragraph).
To insert text at the end of a paragraph, determine the ending point and subtract 1 from this location (the paragraph mark is one character), as shown in the following example.
This example inserts text at the end of the active document. The Content property returns a Range object.
ActiveDocument.Content.InsertAfter "end of document"
This example inserts text at the end of the selection and then collapses the selection to an insertion point.
With Selection
.InsertAfter "appended text"
.Collapse Direction:=wdCollapseDirection.wdCollapseEnd
End With
This example inserts text from an input box as the second paragraph in the active document.
response = InputBox("Type some text")
With ActiveDocument.Paragraphs(1).Range
.InsertAfter "1." & Chr(9) & response
.InsertParagraphAfter
End With
InsertParagraph
Replaces a Range or Selection with a new paragraph.
After this method has been used, the range or selection is the new paragraph.
objRange.InsertParagraph
If you don't want to replace the range or selection, use the Collapse method before using this method.
The InsertParagraphAfter method inserts a new paragraph following a Range or Selection object
InsertParagraphAfter
InsertParagraphBefore
Insert text at start of document
Set objRange = ActiveDocument.Paragraphs(2).Range
objRange.Collapse Direction:=wdCollapseDirection.wdCollapseStart
objRange.Text = "start of document"
or
Set objRange = ActiveDocument.Range(Start:=0, End:=0)
objRange.Text = "start of document"
or
Set objRange = ActiveDocument.Range
objRange.StartOf Unit:=wdUnits.wdStory, _
Extend:=wdMovementType.wdMove
objRange.Text = "start of document"
Insert text to the end of a document
To insert text at the end of a document, you can collapse the range to the end using wdCollapseEnd
Set objRange = ActiveDocument.Paragraphs(2).Range
objRange.Collapse Direction:=wdCollapseDirection.wdCollapseEnd
objRange.Text = "end of document"
or
Set objRange = ActiveDocument.Range
objRange.EndOf Unit:=wdUnits.wdStory, _
Extend:=wdMovementType.wdMove
objRange.Text = "end of document"
To refer to the insertion point immediately after the tenth character, set the character position to 10.
Set objRange = ActiveDocument.Range(10,0)
Set objRange = ActiveDocument.Range(9,9) ??
The end paragraph mark is actually counted as a character when you use:
ActiveDocument.Characters.Count
In general
iTotalChars = ActiveDocument.Characters.Count
Set objRange = ActiveDocument.Range(iTotalChars - 1, iTotalChars - 1)
To insert text into a document without replacing existing text use a Range or Selection object that represents a single location (i.e. whose start and end points are equal).
This is easily obtained from an existing range, using the Collapse method
objRange.Collapse Direction:=wdCollapseDirection.wdCollapseStart
objSelection.Collapse Direction:=wdCollapseDirection.wdCollapseEnd
Deleting Text
Backspace
Selection.TypeBackspace
Selection.Delete Unit:=wdUnits.wdCharacter, Count:=1
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext