Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

independent range objects in Word 1

Status
Not open for further replies.

RobBroekhuis

Technical User
Oct 15, 2001
1,971
US
Allright, so I wasn't born an object-oriented programmer...
Here's what I'm trying to do: I have a range object erng. I need to do some processing around that range, so I want to use a second object rng. I naively thought I could use
Code:
set rng=erng
to create a new independent object, but instead, manipulations of rng affect erng as well. I guess I should have known. But how can I do what I'm trying to achieve? I know this works:
Code:
erng.select
set rng=selection.range
but I'm trying to avoid the selection object. The new keyword doesn't appear to apply to range objects.
Help!
Rob
 
Ron,

What are you trying to achieve? rng & erng are the same thing; the Selection.

Are you wanting to have multiple pointers in this object or something???

Enquiring minds need to know :) Skip,
metzgsk@voughtaircraft.com
 
Try Set NewRange = OldRange.Duplicate

Without the duplicate method, you are just creating two identical pointers to the same thing.
 
Thanks StewartJ - that was exactly what I was looking for. It's still unclear to me why
Code:
set rng=selection.range
works, i.e., why that creates an independent range object that's not connected to the selection.range object.
Rob
 
Because, according to Help...

"By duplicating a Range object, you can change the starting or ending character position of the duplicate range without changing the original range."

It's a matter of handling multiple pointers. :)
Skip,
metzgsk@voughtaircraft.com
 
I understand why the duplicate method works. I don't understand why the direct assignment from the selection object works. I guess that automatically triggers a duplication?
Rob
 
Your variable is a pointer to the range table in VBA. That, in turn, is a pointer to the contents of your Word document. When you Set NewRange = OldRange, all you are doing is creating another pointer to the RANGE TABLE in VBA (which holds the start and ending positions of the range); not directly to the document. Manipulating NewRange changes the contents of the range table. OldRange is still looking at the SAME entry in the range table and so also sees your changes. Using the Duplicate method creates a new, independent, entry in the range table.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top