none
Word VBA range.Setrange redefines other variables? RRS feed

  • Question

  • Word VBA: when i apply range.SetRange to one variable, it also seems to redefine other range variables?

    Below, why does rng2's end change, when rng1 is modified? 

    I expected rng2 would have been unaffected by the .setrange applied to rng1

    Sub testSetRange()
    Dim rng1 As Word.Range
    Dim rng2 As Word.Range
    Set rng1 = ActiveDocument.Words(1)
    Set rng2 = rng1
    rng1.SetRange Start:=rng1.Start, End:=rng1.End + 2
    End Sub


    • Edited by blksg6 Wednesday, May 8, 2019 1:35 PM
    Wednesday, May 8, 2019 1:35 PM

All replies

  • It seems that there is a single Range object, which is referenced by both of rng1 and rgn2.

    If you need separate objects, then try this:

       Set rng2 = ActiveDocument.Range(rng1.Start, rng1.End)

    (Instead of ‘Set rng2 = rng1’).

    • Edited by Viorel_MVP Wednesday, May 8, 2019 2:26 PM
    Wednesday, May 8, 2019 2:25 PM