Range Object

A Range object represents a continuous area in a document.
Each range object is defined by a starting and ending character position.
Range objects are independent of the current selection so you can manipulate a range without changing the current selection.
The Range object is very important as it often provides access to many "properties" of an object.
In many cases, what you might expect to be a property of an object is in fact a property of the object's range object.
Although a Range object doesn't have a visual representation in a document you can, however, use the Select method to select it.
This can be useful when debugging to make sure that the Range object refers to the correct range.


Creating a Range Object

You can use the Range method of a document object to create a Range object representing that document.
Every Range object is defined by a starting and an ending character position.
The following example applies bold formatting to the first 10 characters in the active document.

Dim objRange As Range 
Set objRange = ActiveDocument.Range(Start:=0, End:=10)
objRange.Bold = True

There are several other ways to create a Range object, here are a few more:

Set objRange = ActiveDocument.Paragraphs(2).Range 
Set objRange = ActiveDocument.Paragraphs(2)
Set objRange = ActiveDocument.Sentences(2)
Set objRange = ActiveDocument.Words(2)
Set objRange = ActiveDocument.Characters(2)
Set objRange = ActiveDocument.Tables(1).Rows(1).Range
Set objRange = ActiveDocument.Bookmarks(1).Range
Set objRange = Cells
Set objRange = Rows
Set objRange = Document
Set objRange = Frame
Set objRange = HeadersFooters
Set objRange = Lists
Set objRange = Sections
Set objRange = Selections
Set objRange = ActiveDocument.Find.Execute

The GoTo Method returns the Range object that represents the starting position of the target of the GoTo method.

Set objRange = ActiveDocument.GoTo What:=wdGoToItem.wdGoToLine, _ 
                                   Which:=wdGoToDirection.wdGoToAbsolute, _
                                   Count:=2

Using a Range Object

You do not have to create an object representing the range it is also possible to refer to the range object directly.

ActiveDocument.Range(Start:=0, End:=10).Bold = True 

Similar to a bookmark, a range can span a group of characters or mark a specific location in a document.
If you want a range to represent a specific location then the start and end character positions need to be the same.
The following example creates a range that represents the location at the start of the active document.

Set objRange = ActiveDocument.Range(Start:=0, End:=0) 

You can then use this range object to insert the text "hello" at the start of the document.

objRange.InsertBefore Text:="hello " 

The following example refers to the first three paragraphs in the active document.

Set objRange = ActiveDocument.Range(Start:=0, _ 
                                    End:=ActiveDocument.Paragraphs(3).Range.End)

Redefining a Range Object

You can use the SetRange method to redefine an existing Range object
The following example defines a range object to the be equal to the current selection and then redefines it to refer to the current selection plus the next 10 characters.

Dim objRange As Range 
Set objRange = ActiveWindow.Selection.Range
objRange.SetRange(Start:=objRange.Start, _
                  End:=objRange.End+10)

Default Property (Text)

The Text property is the default property for the Range object

ActiveDocument.Paragraphs(1).Range.Text 
ActiveDocument.Paragraphs(1).Range

Identifying a Range Object

The Start, End and the StoryType properties of a Range object can be used to uniquely identify it.
There are eleven different story types represented by the wdStoryType constants.



© 2022 Better Solutions Limited. All Rights Reserved. © 2022 Better Solutions Limited TopPrevNext