Working with Range Objects in Microsoft Word VBA

Range Objects in Word VBA

Microsoft Word

A common task when using Visual Basic is to specify an area in a document and then do something with it, such as insert text or apply formatting.  For example, a macro may need to locate a word or phrase within a portion of a document.  The portion of the document can be represented by Range Objects in Word VBA.  After the Range object is identified, methods and properties of the Range object can be applied in order to modify the contents of the range.

A Range object refers to a contiguous area in a document.  Each Range object is defined by a starting and ending character position.  Similar to the way bookmarks are used in a document, Range objects are used in Visual Basic procedures to identify specific portions of a document.  A Range object can be as small as the insertion point or as large as the entire document.  However, unlike a bookmark, a Range object only exists while the procedure that defined it is running.

The Start, End and StoryType properties uniquely identify a Range object.  The Start and End properties return or set the starting and ending character positions of the Range object.  The character position at the beginning of the document is zero, the position after the first character is one, and so on.  There are 11 different story types represented by the WdStoryType constants of the StoryType property.

Note that Range objects are independent of the selection.  That is, a range can be defined and modified without changing the current selection.  Multiple ranges can be used in a document, while there is only ever one selection per document pane.

Using the Range Method

The Range method is used to create a Range object in the specified document.  The Range method (which is available from the Document object) returns a Range object located in the main story given a start and end point.  The following example creates a Range object that is assigned to the variable MyRange:

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

MyRange refers to the first ten characters in the active document.  The following example applies bold formatting to the first ten characters in the active document:

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

When a Range object needs to be referred to multiple times, use the Set statement to set a variable equal to the Range object.  However, if only a single action is to be performed on a Range object, there’s no need to store the object in a variable.  The same results can be achieved using just one instruction that identifies the range and changes the Bold property.

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

Like a bookmark, a range can span a group of characters or mark a location in a document.  The Range object in the following example has the same starting and ending points.  The range does not include any text.  The following example inserts text at the beginning of the active document.

Set myRange = ActiveDocument.Range(Start:=0, End:=0)
myRange.InsertBefore “Hello “

Define the beginning and end points of a range using the character position numbers as shown above, or use the Start and End properties with objects such as Selection, Bookmark or Range.  The following example creates a Range object beginning at the start of the second paragraph and ending after the third paragraph.

Set myDoc = ActiveDocument
Set myRange = myDoc.Range(Start:=myDoc.Paragraphs(2).Range.Start, _

Using the Range Property

The Range property appears on multiple objects, such as Paragraph, Bookmark and Cell, and is used to return a Range object.  The following example returns a Range object that refers to the first paragraph in the active document.

Set myRange = ActiveDocument.Paragraphs(1).Range

After a Range object is set, use any of its properties or methods to modify the Range object.  The following example selects the second paragraph in the active document:


To apply numerous properties or methods to the same Range object, use the With…End With structure.  The following example formats the text in the first paragraph of the active document:

Set myRange = ActiveDocument.Paragraphs(1).Range
With myRange
.Bold = True
.ParagraphFormat.Alignment = wdAlignParagraphCenter
.Font.Name = “Arial”
End With

Redefining a Range Object

Use the SetRange method to redefine an existing Range object.  The following example defines myRange to the current selection.  The SetRange method redefines myRange so that it refers to current selection plus the next ten characters:

Set myRange = Selection.Range
myRange.SetRange Start:=myRange.Start, End:=myRange.End + 10


A Story is a document area that contains a range of text distinct from other areas of text in a document.  For example, if a document includes body text, footnotes, and headers, it contains a main text story, footnotes story, and headers story.

There are 11 different types of stories that can be part of a document, corresponding to the following WdStoryType constants:

wdCommentsStory, wdEndnotesStory, wdEvenPagesFooterStory, wdEvenPagesHeaderStory, wdFirstPageFooterStory, wdFirstPageHeaderStory, wdFootnotesStory, wdMainTextStory, wdPrimaryFooterStory, wdPrimaryHeaderStory, and wdTextFrameStory.

The StoryRanges collection contains the first story for each story type available in a document.  Use the NextStoryRange method to return subsequent stories.Visual Basic Training Glasgow EdinburghLearn more – we have courses to suit every level.

Leave a Reply

Your email address will not be published. Required fields are marked *