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.
Range Object - Properties
Bold | Returns True, False or wdUndefined (a mixture of True and False). Can be set to True, False, or wdToggle. |
BookmarkID | Returns the number of the bookmark that encloses the beginning of the specified selection or range; returns 0 (zero) if there's no corresponding bookmark. (Long) |
Bookmarks | Returns a Bookmarks collection that represents all the bookmarks in the range Read-only. |
Borders | Returns a Borders collection that represents all the borders for the specified object. |
Case | Returns or sets a WdCharacterCase constant that represents the case of the text in the range. |
Characters | Returns a Characters collection that represents the characters in the range. |
CharacterWidth | WdCharacterWidth. Returns or sets the character width of the range. |
CombinedCharacters | Returns True or False indicating if the range contains combined characters. |
Comments | Returns a Comments collection that represents all the comments in the range. |
Document | Returns a Document object associated with the specified pane, window, or selection. |
Duplicate | Returns a duplicate range object representing all the properties from this range. |
Editors | Returns an Editors object that represents all the users authorized to modify this range. |
EmphasisMark | WdEmphasisMark - Returns or sets the emphasis mark for a character or designated character string. |
End | Returns or sets the ending character position of the range. Read Only (Long) |
EndnoteOptions | Returns an EndnoteOptions object that represents the endnotes in the range. |
EndNotes | Returns an Endnotes collection that represents all the endnotes in the range. Read-only. |
Fields | Returns Fields collection that represents all the fields in the range. Read Only |
Find | Returns a Find object that contains the criteria for a find operation. Read-only |
FitTextWidth | Returns or sets the width (in the current measurement units) in which Microsoft Word fits the text in the current range. (Single). |
Font | Returns or sets a Font object that represents the character formatting of the range. |
FootnoteOptions | Returns FootnoteOptions object that represents the footnotes in the range. |
Footnotes | Returns a Footnotes collection that represents all the footnotes in the range. Read-only. |
FormattedText | Returns or sets a Range object that includes the formatted text in the range. |
FormFields | Returns a FormFields collection that represents all the form fields in the range. Read-only. |
Frames | Returns a Frames collection that represents all the frames in the range. Read-only |
GrammarChecked | Returns True or False indicating if a grammar check has been run on the range. False is returned if some of the specified range or document hasn't been checked for grammar. |
GrammaticalErrors | Returns a ProofreadingErrors collection that represents the sentences that failed the grammar check on the range. There can be more than one error per sentence. Read-only. |
HighlightColorIndex | Returns or sets the highlight color for the range. (WdColorIndex). |
HorizontalInVertical | Returns or sets the formatting for horizontal text set within vertical text. (wdHorizontalInVerticalType). |
HTMLDivisions | Returns an HTMLDivisions object that represents an HTML division in a Web document. |
Hyperlinks | Returns a Hyperlinks collection that represents all the hyperlinks in the range. Read-only. |
ID | Can be used to define hyperlinks in a document ?? |
Information | Returns information about the specified selection or range. Read-only Variant. |
InlineShapes | Returns an InlineShapes collection that represents all the InlineShape objects in the range. Read-only. |
IsEndOfRowMark | Returns True or False indicating if the range is collapsed and is located at the end-of-row mark in a table. Read-only. |
Italic | Returns True or False or wdUndefined indicating if the range is formatted in italic. (Long) |
LanguageID | Returns or sets the language for the range. |
ListFormat | Returns a ListFormat object that represents all the list formatting characteristics of the range. Read-only. |
ListParagraphs | Returns a ListParagraphs collection that represents all the numbered paragraphs in the range. Read-only. |
NextStroyRange | Returns a Range object that refers to the next story, as shown in the following table. Parameter is from wdStoryType |
NoProofing | True if the spelling and grammar checker ignores the specified text. Returns wdUndefined if the NoProofing property is set to True for only some of the specified text. (Long) |
Orientation | Returns or sets the orientation of text in a range when the Text Direction feature is enabled. WdTextOrientation. |
PageSetup | Returns a PageSetup object that's associated with the specified range. Read-only. |
ParagraphFormat | Returns or sets a ParagraphFormat object that represents the paragraph settings for the range |
Paragraphs | Returns a Paragraphs collection that represents all the paragraphs in the range. Read-only. |
PreviousBookmarkID | Returns the number of the last bookmark that starts before or at the same place as the specified selection or range; returns 0 (zero) if there's no corresponding bookmark. Read-only Long. |
ReadabilityStatistics | Returns a ReadabilityStatistics collection that represents the readability statistics for the range. Read-only. |
Revisions | Returns a Revisions collection that represents the tracked changes in the range. Read-only. |
Scripts | Returns a Scripts collection that represents the collection of HTML scripts in the specified object. |
Sections | Returns a Sections collection that represents the sections in the range. Read-only. |
Sentences | Returns a Sentences collection that represents all the sentences in the range. Read-only. |
Shading | Returns a Shading object that refers to the shading formatting for the specified object. |
ShapeRange | Returns a ShapeRange collection that represents all the Shape objects in the specified range or selection. The shape range can contain drawings, shapes, pictures, OLE objects, ActiveX controls, text objects, and callouts. Read-only. |
ShowAll | True if all nonprinting characters (such as hidden text, tab marks, space marks, and paragraph marks) are displayed. Read/write Boolean. |
SmartTags | Returns a SmartTags object that represents a smart tag in a document |
SpellingChecked | True if spelling has been checked throughout the specified range or document. False if all or some of the range or document hasn't been checked for spelling. Boolean |
SpellingErrors | Returns a ProofreadingErrors collection that represents the words identified as spelling errors in the specified document or range. Read-only. |
Start | Returns or sets the starting character position of a selection, range, or bookmark. Long. |
StoryLength | Returns the number of characters in the story that contains the specified range or selection. Read-only (Long) |
StoryType | Returns the story type for the specified range, selection, or bookmark. Read-only WdStoryType. |
Style | Returns or sets the style for the specified object. To set this property, specify the local name of the style, an integer, a WdBuiltinStyle constant, or an object that represents the style. For a list of valid constants, consult the Microsoft Visual Basic Object Browser. Read/write Variant. |
Subdocuments | Returns a Subdocuments collection that represents all the subdocuments in the specified range or document. Read-only. |
SynonymInfo | Returns a SynonymInfo object that contains information from the thesaurus on synonyms, antonyms, or related words and expressions for the specified word or phrase |
Text | Returns or sets the plain unformatted text in the range. (String). |
TextRetrievalMode | Returns a TextRetrievalMode object that controls how text is retrieved from the specified Range. Read/write. |
TwoLinesInOne | Returns or sets whether Microsoft Word sets two lines of text in one and specifies the characters that enclose the text, if any. Read/write WdTwoLinesInOneType. |
Underline | Returns or sets the type of underline applied to the font or range. Read/write WdUnderline |
Words | Returns a Words collection that represents all the words in a range, selection, or document. Read-only. Note Punctuation and paragraph marks in a document are included in the Words collection. |
XML | Returns a String that represents the XML text in the specified object. |
XMLNodes | Returns an XMLNodes collection that represents the collection of all XML elements within a document or in a selection or range - including those elements that are only partially within the selection or range. |
XMLParentNode | Returns an XMLNode object that represents the parent node of a range. |
Range Object - Methods
© 2024 Better Solutions Limited. All Rights Reserved. © 2024 Better Solutions Limited TopPrevNext