Hi. Does anybody know how to select the largest shape in page?
The problem is the following: I have many shapes within a large rectangle and also some shapes outside the same rectangle.
I want the macro to identify the largest shape in page (the rectangle in this case) and, based on its coordinates, to select all the shapes within the rectangle and group them alltogheter.
Thank you very much!
Instead of looping through each shape, you could use a little CQL to sort the shapes by the area. Then select the first shape which will be the largest. Here is an example:
Sub SelectByLargestArea() Dim sr As ShapeRange Dim sLargest As Shape Set sr = ActivePage.Shapes.All sr.Sort "@shape1.com.DisplayCurve.Area > @shape2.com.DisplayCurve.Area" Set sLargest = sr.FirstShape sLargest.CreateSelection End Sub
Happy Coding,
-Shelby
Thank you Shelby!
I did it almost the same but I've forgoted about the LastShape solution and I used a loop to find the first shape:
For Each s In sr If sr.IndexOf(s) = 1 Then GoTo .... Next s
Is there another way to select a shape by its index avoiding the loop?
After some testing I've discovered that a faster solution is to search for linear dimensions instead of area.
Using:
sr.Sort "@shape1.width > @shape2.width and @shape1.height > @shape2.height"
instead of:
sr.Sort "@shape1.com.DisplayCurve.Area > @shape2.com.DisplayCurve.Area"
gives a faster response.
Thank you again Shelby!
sorin12003 said:@shape1.width > @shape2.width and @shape1.height > @shape2.height"
Draw two rectangles of 2 cm x 4 cm and 3 cm x 3 cm. The area of the second one will be larger, but its width (3) will be less than the width of the first rectangle (4) and maybe it will not be sorted correctly
Shark is correct it would be better to do this:
sr.Sort "@shape1.width * @shape1.height > @shape2.width * @shape2.height"
Using the area will still be more precise, but as you said maybe a little slower, so you may prefer the above.
Happy coding,
thank you