Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

basic question on Range object syntax

25 views
Skip to first unread message

Larry

unread,
Nov 16, 2003, 7:52:06 PM11/16/03
to

This is a really basic and maybe foolish-sounding question about the
syntax of ranges, but it's
something that's never made sense to me so I might as well ask.

Here is sample code that selects from the first through the fifth
character of the current paragraph:

Dim r As Range
Set r = Selection.Range

ActiveDocument.Range(r.Paragraphs(1).Range.Start,
r.Paragraphs(1).Range.Characters(5).End).Select

"ActiveDocument.Range" is the range
that equals the entire document. The range start and end information
inside
the parentheses defines a specific range that runs from the start of the
current paragraph to the end of the fifth character in the current
document. My question is, how does VBA derive from one to the other?
If
ActiveDocument.Range is the range of the whole document, then that's
what it is, isn't it? It seems to me that "ActiveDocument.Range" is
saying one thing, and "(r.Paragraphs(1).Range.Start,
r.Paragraphs(1).Range.Characters(5).End)" is saying another. My point
is, why is "ActiveDocument.Range" necessary at all? What purpose
syntactically does it serve in the code?

Is the idea that any range has to begin with the whole document, and
then its beginning and end is marked out within the document?

This seems to be the case, since I try to do the same thing by starting
like this:

Selection.Paragraphs(1).Range(

When I type the open paren I don't get the prompt to type in the start
of the range. And this is the case, even though
Selection.Paragraphs(1).Range is as much a range as ActiveDocument.Range
is. Yet only the latter will work for having a parenthetical statements
containing the start and end of the range.

Maybe my question could be answered if the VBA code were translated into
descriptive language.

I think I've been partly handicapped in my use of ranges because the
syntax itself has never made complete sense to me.

Thanks for any clarification,
Larry


Jezebel

unread,
Nov 16, 2003, 8:49:55 PM11/16/03
to
I think you bit you are missing is the difference between the Range *object*
and the range *method*.

A range is any continuous stretch of document. ActiveDocument.Range refers
to the entire body of the document (not quite the same as the entire
document, because it doesn't include headers, footers, textboxes, etc --
check Help on the StoryRanges collection).

A range object is defined by its start and end points with the document: it
is simply an object variable referring to the stretch of document. As with
any object variable, you must not only declare it, you also need to create
it. The Range method creates a new range object given two numeric
arguments - namely the start and end points. Very often (as in your example)
these start and end points are expressed as properties of an existing range,
but they needn't be. Eg, your example could be written like this:

Dim pStart as long
Dim pEnd as long
Dim pNewRange as Word.Range

pStart = Selection.Paragraphs(1).Range.Start
pEnd = pStart + 5

Set pNewRange = ActiveDocument.Range(pStart, pEnd)
pNewRange.Select

So, specifically to answer your question, the syntactic function of
'ActiveDocument.Range' is that it is a *method* that returns a Range
*object*.


"Larry" <larr...@att.net> wrote in message
news:OYtcXTKr...@TK2MSFTNGP09.phx.gbl...

JGM

unread,
Nov 16, 2003, 10:29:49 PM11/16/03
to
Hi Larry,

To help drive Jezebel's point home even further, while in the VBA editor
type:

Selection.Range

Then select "Range" and hit F1.

Do the same with

ActiveDocument.Range

Notice the difference?

Cheers!

--
_______________________________________
Jean-Guy Marcil
jma...@sympatico.ca

"Larry" <larr...@att.net> a écrit dans le message de news:
OYtcXTKr...@TK2MSFTNGP09.phx.gbl...

Larry

unread,
Nov 17, 2003, 12:06:24 AM11/17/03
to
The Range in ActiveDocument.Range is a property, not a method. The
range in Selection.Range is an object.

But the description of the Range in ActiveDocument.Range answers my
question, I think:

"Returns a Range object that represents the portion of a document that's
contained in the specified object."

In other words, the syntax starts off with "ActiveDocument" because the
range (defined by the start and end statements in the parentheses
following "Range") is going to be some portion of the document.

To me, it still seems a somewhat confusing or counter-intuitive way of
setting up the syntax. But it does make more sense now. Thanks much.

Larry


Jezebel

unread,
Nov 17, 2003, 12:30:35 AM11/17/03
to
Range Method
Document object: Returns a Range object by using the specified starting and
ending character positions. For information about Range objects, see Working
with Range objects.

Shapes object: Returns a ShapeRange object that represents a subset of
shapes in the Shapes collection.

Syntax 1

expression.Range(Start, End)

Syntax 2

expression.Range(Index)

"Larry" <larr...@att.net> wrote in message

news:uA7hZhMr...@TK2MSFTNGP12.phx.gbl...

JGM

unread,
Nov 18, 2003, 1:41:40 AM11/18/03
to
Hi Larry,

Sorry, but I think you got it mixed up...


> The Range in ActiveDocument.Range is a property, not a method. The
> range in Selection.Range is an object

At least, that's what my version of Word is telling me when I type
ActiveDocument.Range
Selection.Range
and then select "Range" and hit F1...

Also,


> But the description of the Range in ActiveDocument.Range answers my
> question, I think:
>
> "Returns a Range object that represents the portion of a document that's
> contained in the specified object."

The definition you quote is from the Selection.Range property definition,
not the ActiveDocument.Range one.

From Word's help, "Range" in ActiveDocument.Range is a method, and Word
provides the following example:

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

or with ShapeRange

Sub SelectShapeRange()
ActiveDocument.Shapes.Range(Array(1, 3)).Select
End Sub

And you will see that "Selection" is not in the "Applies to " list.

"Range" in Selection.Range IS a property, as an example form Word's help
file:

ActiveDocument.Paragraphs(1).Range.Style = wdStyleHeading1

And you will see that "Document" is not in the "Applies to " list.


Finally,


> In other words, the syntax starts off with "ActiveDocument" because the
> range (defined by the start and end statements in the parentheses
> following "Range") is going to be some portion of the document.

This is not a very clear way of looking at it because a range object is
always going to be a portion of the document, or the whole document.

Another way of looking at the difference is that with the method, you define
the range after the word "Range" in the code,
(Word says something about returning a range object by using the specified
starting and ending charaters)
Also, it does not have to be the active document, any document object will
take the Range method, which allows you to manipulate text from many
document simultaneously, which is more difficult with Selection.Range.

Whereas with the property, the bit before the word "Range" defines the
range.


> "Returns a Range object that represents the portion of a document that's
> contained in the specified object."

i.e., the object defined by the bit before the word Range.
Personally, I almost only use "Selection.Range" to define a range object
based on the current selection so I can call it up anywhere in my code..
As in:
Dim MyRange as Range
Set MyRange = Selection.Range

'(some code)

MyRange.Copy

'and at the end, to return the "cursor" as it was

MyRange.Select
Set MyRange = Nothing

Anyway, that's only my take on this!

Cheers!
______
Jean-Guy Marcil
jma...@sympatico.ca

"Larry" <larr...@att.net> a écrit dans le message de news:

uA7hZhMr...@TK2MSFTNGP12.phx.gbl...

Larry

unread,
Nov 18, 2003, 3:10:55 PM11/18/03
to

JGM,

There's a lot here to absorb, but for the moment I just want to reply to
one thing. In my Word 97 VBA, if I put cursor in the "Range" of
ActiveDocument.Range and type F1, I get a Help page that says

"Range Property":

"Returns a Range object that represents the portion of a document that's

contained in the specified object. Read-only."

The very same thing happens if I type Selection.Range, put the cursor in
"range" and type F1.

Repeat: I get the same Help page for Selection.Range and for
ActiveDocument.Range.

Larry

JGM

unread,
Nov 18, 2003, 5:19:52 PM11/18/03
to
Hi Larry,

Yes, I tried on my Word 97 (I have 4 versions of Word at home, but I usually
work on Word XP) and got the same result as you did...
I guess Word 97 is not as good as Word XP at reading the context...

In Word 97, type
ActiveDocument.Range

Then select "Range" and hit F1.

Then click in on the "See also" link, and you will see "Range, Method"
listed.

Have a good day!

Cheers.


--
_______________________________________
Jean-Guy Marcil
jma...@sympatico.ca

"Larry" <larr...@att.net> a écrit dans le message de news:

OYkFf$grDHA...@TK2MSFTNGP10.phx.gbl...

Larry

unread,
Nov 19, 2003, 9:29:56 PM11/19/03
to

Thanks, I think I understand it better now. The Range method always
works on a Document object, i.e., on a document. That's why the syntax
always involves ActiveDocument or some named document or a variable that
represents a document. And out of that entire document object, the
Range method delineates a range.

It's basic to the syntax and the meaning of a method. "Range" in
ActiveDocument.Range is not something that _belongs_ to the
ActiveDocument, that is, it is not the range corresponding to the whole
document (which was what I had thought and what confused me). Rather,
"Range" is the method that is _operating on_ the ActiveDocument.

Meanwhile, the "Range" is Selection.Range is the property of the
selection that returns a range object equal to the selection object.

The obvious source of the confusion is similar-looking structure of
ActiveDocument.Range and Selection.Range.

To add a further twist, the Range in ActiveDocument.Range can also be a
range property, as in:

ActiveDocument.Range.Select

Thanks again.
Larry

Larry

unread,
Nov 19, 2003, 9:39:49 PM11/19/03
to
The thing is, being just an amateur with VBA, and creating lots of
macros for myself, I never really studied it properly. It's like
learning a language without having studied grammar. You may be able to
speak well, but you can't _explain_ what you're doing. For that, you
need to know the grammar and syntax, learn to parse sentence, and so on.
As soon as I started playing with VBA it would have been better if I got
a book that went through it in a systematic way, instead of picking it
up in bits and and pieces and never really _understanding_ it.

Larry


Jezebel

unread,
Nov 19, 2003, 9:43:42 PM11/19/03
to
By jove, I think he's got it. For your next adventure in Ranges, explore the
StoryRanges collection. That gets you to all the bits of the document that
aren't part of the body text (headers and footers, etc).


"Larry" <larr...@att.net> wrote in message

news:ua1Ua9wr...@TK2MSFTNGP12.phx.gbl...

Larry

unread,
Nov 20, 2003, 7:23:43 PM11/20/03
to

Yes, me and Eliza Doolittle. :-)

However, I just came upon this in a VBA help file called "Understanding
objects, properties, and methods," which I think gives an idea of why
learning these terms can be more difficult than it ought to be.

"A method is AN ACTION THAT AN OBJECT CAN PERFORM. For example, just as
a document can be printed, the Document object has a PrintOut method."

But, as I致e now learned from this discussion, I think this is
incorrect. I would say that PrintOut is something that is being DONE TO
the document object, not something that the document object is DOING.
In the same way, the Range method in ActiveDocument.Range (Start, End)
is something that is being DONE TO the active document, not being done
by it, that is, operating on the whole document, the method delineates
out of the document a range with a start and end point.

But then the same Help file turns around and says just the opposite of
what it said above:

"In most cases, methods are actions and properties are qualities. Using
a method CAUSES SOMETHING TO HAPPEN TO AN OBJECT ... " [my emphasis]

This kind of confusion in the definition of basic concepts adds to the
difficulty of grasping VBA.

Larry

Jay Freedman

unread,
Nov 20, 2003, 8:20:13 PM11/20/03
to
Hi, Larry,

The philosophy is getting a little deep, but I'll put on my waders...

As an analogy, think about what happens when your doctor taps your
knee with a rubber mallet and your leg swings up. Was something done
to you (the tap) or did you do something (the leg movement)?

It's very common in object-oriented programming to talk about telling
an object to do something to itself -- print, save, copy, etc. The
code to perform the action is actually part of the definition of the
object. This is one of the major differences between object-oriented
languages and the older "procedural" languages.

So I would say that invoking the ActiveDocument.PrintOut method causes
the document object to print itself.


"Larry" <larr...@att.net> wrote:

>Yes, me and Eliza Doolittle. :-)
>
>However, I just came upon this in a VBA help file called "Understanding
>objects, properties, and methods," which I think gives an idea of why
>learning these terms can be more difficult than it ought to be.
>
>"A method is AN ACTION THAT AN OBJECT CAN PERFORM. For example, just as
>a document can be printed, the Document object has a PrintOut method."
>
>But, as I致e now learned from this discussion, I think this is
>incorrect. I would say that PrintOut is something that is being DONE TO
>the document object, not something that the document object is DOING.
>In the same way, the Range method in ActiveDocument.Range (Start, End)
>is something that is being DONE TO the active document, not being done
>by it, that is, operating on the whole document, the method delineates
>out of the document a range with a start and end point.
>
>But then the same Help file turns around and says just the opposite of
>what it said above:
>
>"In most cases, methods are actions and properties are qualities. Using
>a method CAUSES SOMETHING TO HAPPEN TO AN OBJECT ... " [my emphasis]
>
>This kind of confusion in the definition of basic concepts adds to the
>difficulty of grasping VBA.
>
>Larry
>


--
Regards,
Jay Freedman
Microsoft Word MVP FAQ: http://www.mvps.org/word

Larry

unread,
Nov 21, 2003, 12:20:02 AM11/21/03
to

Jay,

My question may have been deep, but you were more than up to the
challenge. :-)

By the same token, I guess we could say that the
ActiveDocument.Range(Start, End) method causes the document to delineate
the specified portion of itself as a range.

Thanks again.

Larry

"Jay Freedman" <jay.fr...@verizon.net> wrote in message
news:b2qqrv89iasat4t5m...@4ax.com...


> Hi, Larry,
>
> The philosophy is getting a little deep, but I'll put on my waders...
>
> As an analogy, think about what happens when your doctor taps your
> knee with a rubber mallet and your leg swings up. Was something done
> to you (the tap) or did you do something (the leg movement)?
>
> It's very common in object-oriented programming to talk about telling
> an object to do something to itself -- print, save, copy, etc. The
> code to perform the action is actually part of the definition of the
> object. This is one of the major differences between object-oriented
> languages and the older "procedural" languages.
>
> So I would say that invoking the ActiveDocument.PrintOut method causes
> the document object to print itself.
>
>
> "Larry" <larr...@att.net> wrote:
>
> >Yes, me and Eliza Doolittle. :-)
> >
> >However, I just came upon this in a VBA help file called
"Understanding
> >objects, properties, and methods," which I think gives an idea of why
> >learning these terms can be more difficult than it ought to be.
> >
> >"A method is AN ACTION THAT AN OBJECT CAN PERFORM. For example, just
as
> >a document can be printed, the Document object has a PrintOut
method."
> >

> >But, as I've now learned from this discussion, I think this is

Chip Orange

unread,
Nov 21, 2003, 3:46:40 PM11/21/03
to
I don't understand your point, can't a property also be an object?


"Larry" <larr...@att.net> wrote in message

news:uA7hZhMr...@TK2MSFTNGP12.phx.gbl...

Larry

unread,
Nov 22, 2003, 12:14:27 AM11/22/03
to
I don't think so. A property is a property of an object. In any given
situation, Range can be an object, a property or a method, but it can
only be one of those in any given situation.

Larry


Jonathan West

unread,
Nov 22, 2003, 4:38:11 AM11/22/03
to

"Larry" <larr...@att.net> wrote in message
news:OEOXEdL...@TK2MSFTNGP11.phx.gbl...

> I don't think so. A property is a property of an object. In any given
> situation, Range can be an object, a property or a method, but it can
> only be one of those in any given situation.

That is not so. An object can be a property of another object. For instance,
the PageSetup object is a property of the Document object.


--
Regards
Jonathan West - Word MVP
http://www.multilinker.com
Please reply to the newsgroup

Lars-Eric Gisslén

unread,
Nov 22, 2003, 8:11:59 AM11/22/03
to
Larry,

In general you can look at it this was; if it's a Range property you will
get the pointer to an existing Range object, if it's a method a new range
object will be created with the parameters you pass and the pointer to the
new range object will be returned. This is quite basic to OOP.

--
Regards,
Lars-Eric Gisslén
**Reply only to this Newsgroup**
Any reply as private email will only generate
a spam complaint to your ISP!

"Larry" <larr...@att.net> skrev i meddelandet
news:OEOXEdL...@TK2MSFTNGP11.phx.gbl...

Larry

unread,
Nov 23, 2003, 1:48:58 AM11/23/03
to
Sorry! I thought I understand at least that much. Should have kept my
mouth shut. :-)

Larry


Klaus Linke

unread,
Nov 24, 2003, 8:33:26 AM11/24/03
to
"Jonathan West" <jw...@mvps.org> wrote:
>
> "Larry" <larr...@att.net> wrote in message
> news:OEOXEdL...@TK2MSFTNGP11.phx.gbl...
> > I don't think so. A property is a property of an object.
> > In any given situation, Range can be an object, a property
> > or a method, but it can only be one of those in any given situation.
>
> That is not so. An object can be a property of another object. For
> instance, the PageSetup object is a property of the Document object.


This may be splitting hairs, but I tend to agree with Larry.

As the help file says, the Document object has a PageSetup property that
*returns* (rather than *is*) a PageSetup object.

For me, you can make an object do tricks (methods) and can make it return
internal info (properties).
The trick may be to return a Range object, in which case the method will be
called Range method.
The info returned can be a Range object, in which case the property will be
called Range property.

But it's still different things we are talking about in my view.

Regards,
Klaus


Jonathan West

unread,
Nov 24, 2003, 9:01:05 AM11/24/03
to

"Klaus Linke" <fotosatz...@t-online.de> wrote in message
news:%23wURW%23osDH...@tk2msftngp13.phx.gbl...

As far as I can see, there is little if any practical difference between a
read-only property that returns either is simple value or an object, and a
method that returns the same simple value or object.

There is also little practical difference between a read-write property and
a matched pair of methods which read and set the value of some internal
object or variable.

The only difference between a property and a method/method pair is the
syntax in the language used to express it. Within the Word object model,
there is a convention that properties return information of some aspect of
the object, while methods return values or objects that are related in some
way to the object, or which return information about the operation performed
on the object by the method. But it is important to realise that this is
simply a convention - that in VBA at least it appears that properties and
methods are largely interchangeable.

0 new messages