Arrange Objects Stacking Order by Selection?

I have been looking into the zOrder thing. But I'm not 100% sure that is what I need parse. 

I am trying to stack my objects in the order I have them selected. But of course i'm stuck. 

Odd are - I'll have a lot of objects on the page. But out of all the objects I have 3 selected. Now these objects are scattered through the stacking order. I'd like to probably add my selection to a  new layer in the order I have them selected. 

So, I've started here.

Dim sr As New ShapeRange
Set sr = ActiveSelectionRange

If sr.Count >= 1 Then
ActivePage.Layers("My Selection").Activate
sr.MoveToLayer ActiveLayer ' My selection has moved to it's own layer.

'Here is what I cannot figure out. How to stack my objects in the order I have them selected Under my New layer called "My Selection"

End If

Parents
  • Try code below if it does what you are looking for
    Sub test()
    Dim sr As New ShapeRange
    Set sr = ActiveSelectionRange
    If sr.Count >= 1 Then
    ActivePage.Layers("My Selection").Activate
    For Each s In sr.ReverseRange
    s.MoveToLayer ActiveLayer ' My selection has moved to it's own layer.
    Next s
    End If
    End Sub
Reply
  • Understanding Shape Order



    When you marquee select a ShapeRange, the order you will loop through shapes is from lowest in the stack order to the highest. If you are looking in the Object Manager it will be bottom to top.

    Sub ShowShapeOrder()
        Dim sr As ShapeRange
        Dim s As Shape
        Dim strOrder As String
        
        'Set sr = ActiveSelectionRange
        Set sr = ActiveSelectionRange.ReverseRange
    
        For Each s In sr.Shapes
            If s Is sr.FirstShape Then
                strOrder = s.Name
            Else
                strOrder = strOrder & ", " & s.Name
            End If
        Next s
        
        MsgBox strOrder
    End Sub
    

    The above code would return:

    Blue, Cyan, Green, Yellow

    If you shift select the shapes from left to right the order will be returned as:

    Yellow, Green, Cyan, Blue

    In other words they are returned from Last selected to First selected.

    Often you may want the shapes to be retuned in reverse order. So in my code sample above comment out the Set sr = ActiveSelectionRange line and uncomment the Set sr = ActiveSelectionRange.ReverseRange this time the code will return:

    Yellow, Green, Cyan, Blue

    In the shift selection, if you selected them from Left to right, they would then be returned as:

    Blue, Cyan, Green, Yellow

    What if I want them returned from left to right when doing a marquee selection. This is where sort comes in really handy. With the following code the shapes will be returned left to right:

    Sub SortExample()
        Dim sr As ShapeRange
        Dim s As Shape
        Dim strOrder As String
        
        Set sr = ActiveSelectionRange
        
        'Left to Right
        sr.Sort "@shape1.Left < @shape2.Left"
        
        'Right to Left
        'sr.Sort "@shape1.Left > @shape2.Left"
        
        'Sort by size, smallest to largest
        'sr.Sort "@shape1.width * @shape1.height < @shape2.width * @shape2.height"
    
        For Each s In sr.Shapes
            If s Is sr.FirstShape Then
                strOrder = s.Name
            Else
                strOrder = strOrder & ", " & s.Name
            End If
        Next s
        
        MsgBox strOrder
    End Sub
    


    Blue, Cyan, Green, Yellow

    If you wanted to return the shape order from right to left just change the less than sign to a greater than sign.

    The sort command is very powerful and useful. I often need to return shapes in their size order. If I wanted to return shapes from smallest to largest I could do something like this:

    sr.Sort "@shape1.width * @shape1.height < @shape2.width * @shape2.height"
    

    I hope that helps everyone understand a little more about shape order.

    -Shelby

Children