Bra Utbildning AB

 

<-- Back
VBA & Object models
Work with text
Bookmarks
Ranges
Fields
Form fields
Autotext
Doc var & Doc Prop
Built-in commands
Built-in dialogs
Automation
External databases

Insert text into the document


You can address many different objects (words, paragraphs, headers etc) in Word and you can modify each addressed object as described below. You do not have to go about like the old versions where you first would have to go to/select the object to modify before you could make any changes to it. 

ActiveDocument.Words(1).Text = "FirstWord1"
ActiveDocument.Paragraphs(1).Range.Text = "My first paragraph's text."
ActiveDocument.Bookmarks("AnyBookmarkName").Range.Text = "whatever"
ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range.Text = "Text in the header"
ActiveDocument.Tables(1).Columns(3).Cells(1).Range.Text = "whatever"


Where possible avoid using the Selection object. It is considered faster running code with the method described above.
Selection.InsertAfter "whatever"
Selection.InsertAfter Text:="whatever"
Selection.TypeText "whatever"
Selection.TypeText Text:="whatever"