Help me understand a "range" object

R

Robert Crandal

The Microsft MSDN website contains the following statement
regarding a range object:

1) "The Range object represents a contiguous area in a document, and is
defined by a starting character position and an ending character
position."

My question here is, which parts of a document are NOT "continguous"???
Since a range contains spaces, tab characters and paragraphs marks, it
seems to me that an entire document will always be contiguous. Also,
on a different topic, is it possible to use a range object to access the
middle
of a sentence?? (I understand that a range has a start and end position,
but what if I want to move/offset the range to access a middle sentence?)

thankx
 
J

Jay Freedman

The mention of "continuous" is meant to tell you that a Range object
cannot simultaneously include two or more parts of a document that are
not contiguous. For example, if there are three paragraphs in the
document, a Range object can represent paragraphs 1 and 2, or
paragraphs 2 and 3, or all three paragraphs; but it cannot represent
paragraphs 1 and 3 while excluding paragraph 2.

A user can select noncontiguous areas by holding the Ctrl key while
selecting bits here and there; but VBA can't do much with that kind of
selection (http://support.microsoft.com/?kbid=288424).

Yes, you can assign a Range to represent anything in a document from a
single point (zero length, or .End = .Start) up to the entire
document. It can cover a single character, a few characters in the
middle of a word, a whole word, half of one word and half of the next
word, etc...

Let's say you start by picking a whole sentence:

Set myRange = ActiveDocument.Sentences(3)

(Note that there is no such thing as a Sentence object; the expression
ActiveDocument.Sentences(3) returns a Range object.) Now you can
reduce that range in any of several ways. If you want the fourth word
in that sentence, use

Set myRange = myRange.Words(4)

If you want to move the end of the range toward the start by three
characters, use

myRange.MoveEnd Unit:=wdCharacter, Count:=-3

or

myRange.End = myRange.End - 3

If you want to move the start of the range toward the end by three
characters, use

myRange.MoveStart Unit:=wdCharacter, Count:=3

or

myRange.Start = myRange.Start + 3

To move both the start and the end at the same time, you can do
something like this:

Set myRange = ActiveDocument.Range( _
Start:=myRange.Start + 3, _
End:=myRange.End - 3)

For more ways to manipulate Range objects, look at the VBA Help topic
about Range and then follow the links to the ones about its methods.

--
Regards,
Jay Freedman
Microsoft Word MVP
Email cannot be acknowledged; please post all follow-ups to the
newsgroup so all may benefit.
 
R

Robert Crandal

Jay Freedman said:
If you want to move the end of the range toward the start by three
characters, use

myRange.MoveEnd Unit:=wdCharacter, Count:=-3

Does this mean that a range a current file position or pointer
or something that indicates where the range begins??? If so,
how can I determine or set the current position??

Thank you!

Robert Crandal
 
G

Greg Maxey

Robert,

One thing Jay said that I think need clearing up. While you can do this:

Dim oRng as Word.Range
Set oRng = ActiveDocument.Range

That does not mean that the range defined emcompasses all of the text and
other components in the document. That is sort of a short cut for defining
the range to all of the text in the main text (or body) of the document.

Here is a demo. Open a new blank document and paste in the following text:

Alpha . document main text . omega



Open the header area and paste in the following text:



Primary header text





Paste and run this code in the VB Editor:



Sub ScratchMaco()
Dim oRng As Word.Range
'Define range = to full span of the active document main text
Set oRng = ActiveDocument.Range
MsgBox oRng.Text
'Define range = to full span of section 1 primary header text
Set oRng = ActiveDocument.Sections(1).Headers(wdHeaderFooterPrimary).Range
MsgBox oRng
'Define range = to full span of active document main text story range
Set oRng = ActiveDocument.StoryRanges(wdMainTextStory)
MsgBox oRng.Text
'Work on just part of the defined range
MsgBox oRng.Words.First
'Adjust the scope of the range (chop off the paragraph mark)
oRng.MoveEnd wdCharacter, -1
MsgBox oRng.Words.Last
'Work with the range.
With oRng.Find
.Text = "Alpha"
.Replacement.Text = "First"
.Execute Replace:=wdReplaceAll
.Text = "omega"
.Replacement.Text = "last"
.Execute Replace:=wdReplaceAll
End With
'Do what Jay indicated.
oRng.MoveEnd Unit:=wdCharacter, Count:=-3
MsgBox oRng.Text
End Sub
 
J

Jay Freedman

Does this mean that a range a current file position or pointer
or something that indicates where the range begins??? If so,
how can I determine or set the current position??

Thank you!

Robert Crandal

When you define a Range object, it has properties named .Start and
..End that are the number of characters (sort of) from the beginning of
the document. I said "sort of" because it includes some things you
can't normally see on screen (such as paragraph marks) but doesn't
contain a recognizable count of characters for certain fields,
graphics, text boxes, etc.

Once you get use to working with ranges, you'll find that you can
usually ignore the actual values of the .Start and .End properties;
all you're interested in is the relative locations of things within
the range, or maybe not even those. For example, if you use the
myRange.Find.Execute method to locate occurrences of specific text to
replace or reformat, you don't care where those occurrences are; all
you need to know is that myRange points to them.

But if you really want to set the beginning of a range, yes, you can:

myRange.Start = 25

or some other number. Experiment with it...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top