InsertBefore Method

Microsoft Word Visual Basic

InsertBefore Method

       

Inserts the specified text before the specified selection or range. After the text is inserted, the selection or range is expanded to include the new text. If the selection or range is a bookmark, the bookmark is also expanded to include the next text.

expression.InsertBefore(Text)

expression   Required. An expression that returns a Range or Selection object.

Text   Required String. The text to be inserted.

Remarks

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.

Example

This example inserts the text "Hamlet" (enclosed in quotation marks) before the selection and then collapses the selection.

With Selection
    .InsertBefore Chr(34) & "Hamlet" & Chr(34) & Chr(32)
    .Collapse Direction:=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:=wdCollapseEnd
        .TypeParagraph
    End With
Next aFont