Consulting

Results 1 to 4 of 4

Thread: Help With a Range Concept

  1. #1
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location

    Help With a Range Concept

    I need help with a range concept that I can't seem to understand. I've got the following code where I'm setting the range to another range.

    Sub TestMacro() 
    
        Dim oRng, oNewRng As Range 
        Set oRng = ActiveDocument.Paragraphs(1).Range 
        
        Set oNewRng = oRng
    
        oNewRng.MoveStartUnitl cset:= " ", Count:=wdForward 'Moves the start of the range to the first space that is found
    
        oRng.Select 'For testing purposes only
    
        oNewRng.Select 'For testing purposes only
    
    End Sub
    I thought the range in oRng would maintain the same range but it follows the oNewRng and both are exactly the same (oRng.select and oNewRng.select highlight the same part of the paragraph).

  2. #2
    If you set the range to another range it remains with the other range. set the ranges to the same fixed location

    Set oRng = ActiveDocument.Paragraphs(1).Range
    Set oNewRng = ActiveDocument.Paragraphs(1).Range
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    Microsoft Word MVP 2003-2009 VBAX Guru gmaxey's Avatar
    Joined
    Sep 2005
    Posts
    3,340
    Location
    heedaf,

    A couple of points.

    1. When you post code, please post code that will compile.
    2. When your declaration statements look like this:
    Dim oRng, oRngNew as Range
    You have declared 1 variant type variable and 1 range type variable. Use:
    Dim oRng as Range, oRngNew as Range
    3. Like Graham said or use Duplicate


    Sub TestMacro()
    Dim oRng As Range, oNewRng As Range
      Set oRng = ActiveDocument.Paragraphs(1).Range
      Set oNewRng = oRng.Duplicate
      oNewRng.MoveStartUntil cset:=" ", Count:=wdForward
      oRng.Select
      oNewRng.Select
    End Sub
    Greg

    Visit my website: http://gregmaxey.com

  4. #4
    VBAX Contributor
    Joined
    Jun 2014
    Posts
    107
    Location
    Thank you for the replies. Duplicate was what I was looking for - can't believe I forgot about it.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •