I would like to reorder (z-order) my 'lassoed' selection of shapes based on their position. My current macro ignores their position and the loop loops through the shapes based on their existing z-order which defeats the purpose of the loop!
For example I have 3 shapes in a grid with '1 row' and '3 columns'. When I lasso select the shapes with my mouse and run this macro, I want the left most shape to the in the front, the next shape (middle shape) to be one z-order behind, and the right most shape to then be one z-order behind of the middle shape.
Current code below:
Dim shp As Shape
For each shp in Activewindow.Selection.ShapeRange
shp.zorder msoBringToFront
Next
As far as I understand, the main task is to sort a list of shapes by position.
The following function will sort a list of shapes and return an array of shapes in the correct order. You can call it either with a ShapeRange or with Shapes as argument: Use
ShapeRangefor selection or useslide.Shapesto deal with all shapes of a slide.The function uses a simple Quicksort to sort by the position of the shapes. I use the
round-function because the position of the shapes might be slightly different, even if you align them.Now with the resulting array you can do whatever you want. The following routine will sort the shapes of a slide and write the index as text:
Of course, you can use it also to set the zOrder: