basic question on Range object syntax

L

Larry

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
 
J

Jezebel

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*.
 
J

JGM

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!
 
L

Larry

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
 
J

Jezebel

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)
 
J

JGM

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
(e-mail address removed)
 
L

Larry

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
 
J

JGM

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.
 
L

Larry

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
 
L

Larry

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
 
J

Jezebel

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).
 
L

Larry

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
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
 
J

Jay Freedman

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 said:
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
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
 
L

Larry

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 said:
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 said:
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
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
 
L

Larry

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
 
J

Jonathan West

Larry said:
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.
 
L

Lars-Eric Gisslén

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!
 
K

Klaus Linke

Jonathan West said:
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
 

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