There are several ways to assign an existing Range
object to a variable. This topic explains the results of two different techniques. In the following examples, the
and
variables refer to
Range objects. For example, the following instructions assign the first and second words in the active document to the
and
variables.
Set Range1 = ActiveDocument.Words(1)
Set Range2 = ActiveDocument.Words(2)
|
Setting a Range object variable equal to another Range object variable
This following instruction assigns a range variable named
to represent to the same location as
.
You now have two variables that represent to the same range. When you manipulate the start or endpoint or the text of
, it affects
and vice versa.
Note that the following instruction is the same as
Range2.Text = Range1.Text
|
. This instruction assigns the default property of
, which is the
Text
property, to the default property of
. It doesn't change what the objects actually refer to.
The ranges (
and
) have the same contents, but they may point to different locations in the document or even different documents.
Using the Duplicate property
The following instruction creates a new duplicated Range object,
, which has the same start and endpoints and text as
.
Set Range2 = Range1.Duplicate
|
If you change the start or endpoint of
, it doesn't affect
and vice versa. Because these two ranges point to the same location in the document, changing the text in one range affects the text in the other range.