Selection.Range question

J

Joe

I am using Word 2002 and am new to Word VBA programming (have done plenty of VBA, etc otherwise). I find Selection.Range to be confusing. If I have the following

Selection.TypeText "This is Joe's text", then does Selection.Range refer to the whole sentence? How is this when, after I have finished the sentence and I have not selected anything, the selection object would refer to the insertion point just after the word text

Any hep in understanding this would be greatly appreciated

Thanks

Joe
 
E

Ed

Hi, Joe. As I understand and use it, Selection.Range refers to the range
encompassed by what you have selected. Double-click a word - your Range
includes all the highlighted area, which is your Selection. If you want to
work with the Range of
Selection.TypeText "This is Joe's text"
then you would have to
Selection.MoveRight Unit:=wdWord, Count:=4, Extend:=wdExtend
to select the text. That Selection has a Range connected with it, which now
covers all the inserted text. If you're going to do something with the text
that might change the start and/or end of the Selection, and thus change the
scope of your Range, it might be good to
Set rngWork = Selection.Range.

HTH
Ed

Joe said:
I am using Word 2002 and am new to Word VBA programming (have done plenty
of VBA, etc otherwise). I find Selection.Range to be confusing. If I have
the following:
Selection.TypeText "This is Joe's text", then does Selection.Range refer
to the whole sentence? How is this when, after I have finished the sentence
and I have not selected anything, the selection object would refer to the
insertion point just after the word text?
 
J

Joe

Hi Ed

What if I don't execute Selection.MoveRight Unit:=wdWord, Count:=4, Extend:=wdExtend? Does Selection.Range refer to the insertion point

Jo

----- Ed wrote: ----

Hi, Joe. As I understand and use it, Selection.Range refers to the rang
encompassed by what you have selected. Double-click a word - your Rang
includes all the highlighted area, which is your Selection. If you want t
work with the Range o
Selection.TypeText "This is Joe's text
then you would have t
Selection.MoveRight Unit:=wdWord, Count:=4, Extend:=wdExten
to select the text. That Selection has a Range connected with it, which no
covers all the inserted text. If you're going to do something with the tex
that might change the start and/or end of the Selection, and thus change th
scope of your Range, it might be good t
Set rngWork = Selection.Range

HT
E

Joe said:
I am using Word 2002 and am new to Word VBA programming (have done plent
of VBA, etc otherwise). I find Selection.Range to be confusing. If I hav
the followingto the whole sentence? How is this when, after I have finished the sentenc
and I have not selected anything, the selection object would refer to th
insertion point just after the word text
 
E

Ed

I believe so. But what you can do with it then depends on what's included
in the range. For obvious example, just because Tables are a property of
the Range object doesn't mean I can use a Table command if the Range doesn't
include a table. If the Range is only a single insertion point, then what
you can actually do there may be very limited.

If you're going to do a lot with Word, you might consider picking up the
Word 2000 (or 2002) Developer's Handbook. It breaks out all the objects,
properties, and methods in Word. Explanations and examples aren't the
greatest, but there's still a lot there that I find very helpful.

HTH
Ed

Joe said:
Hi Ed,

What if I don't execute Selection.MoveRight Unit:=wdWord, Count:=4,
Extend:=wdExtend? Does Selection.Range refer to the insertion point?
 
L

Larry

Joe,

After you run the code to type the sentence, both the selection and the
range of the selection would be the insertion point at the end of the
sentence. You could demonstrate for yourself this by running this code.
We're setting the Selection.Range as soon as the text is typed, then
moving the cursor to the start of the document just to move it someplace
else, then selecting the range. The insertion point returns to the end
of the typed sentence.

Dim r As Range
Selection.TypeText "This is Joe's text"
Set r = Selection.Range
Selection.HomeKey wdStory
r.Select

Larry
 

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