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

Work with the range object


A range is an addressable area/object. Where possible, try to use ranges. Old macros required that you first would (physically) select an object, then modify it. With the range object you define which object you are interested in, followed by the action to take. This means that you don't have to go to/jump around in the document to modify it.

A range could be the first or 17th paragraph or it could cover the area starting at word number 15 continuing to the end of the document. 

Set oRng = ActiveDocument.Paragraphs(1).Range
Set oRng = ActiveDocument.Bookmarks("TheBookmarkName").Range


Example
oRng.Delete                      'delete the range
oRng.End = oRng.End - 1  'decrease the size of the range with 1 character

Here an example where the range covers the area from paragraph 9 in the document to the end of the document (to the built-in bookmark \EndOfDoc)

Dim
oDoc As Document
Dim myRange As Range
Set oDoc = ActiveDocument
Set myRange = _
   oDoc.Range(oDoc.Paragraphs(9).Range.Start, _
   oDoc.Bookmarks("\EndOfDoc").Range.End)
myRange.Delete