Hello Everyone,
is it possible to count all objects in an activelayer, also those which are grouped.
If i had one group in the activelayer and i use the vba-code
Set sr = ActivePage.ActiveLayer.Shapes.All
For Each s In sr i = i + 1 next s
msgbox("Number of elements:" &i)
I count only "1" element although the group contais f.e. 9 objects.Is there a solution without ungroup the group?Thanks in advance!
You can look at the Shapes.FindShapes method. It has the option (active by default) to be recursive, so it will get shapes within groups.
Note that groups themselves are considered to be shapes ("group shapes"), so letting it find everything may not produce the count of shapes that you might expect:
Sub count_shapes_1() Dim sr As ShapeRange Set sr = ActivePage.ActiveLayer.Shapes.FindShapes() MsgBox sr.Count End Sub
You could refine the search by including a query, e.g., excluding group shapes, or specifying only "simple shapes":
Sub count_shapes_2() Dim sr As ShapeRange Set sr = ActivePage.ActiveLayer.Shapes.FindShapes(, , , "@com.issimpleshape = 'true'") MsgBox sr.Count End Sub
Try the code below. You will easily understand how to count shapes in a layer, including groups
Sub Macro1() Dim SR As ShapeRange, s As Shape, SRG As ShapeRange Dim i%
Set SR = ActivePage.ActiveLayer.Shapes.All Set SRG = ActivePage.ActiveLayer.FindShapes(, cdrGroupShape)
i = SR.Count - SRG.Count
For Each s In SRG i = i + s.Shapes.Count Next s
MsgBox "Number of elements:" & i
End SubRegardsTaras
Hello Eskimo and Taras.SoftLV,
thank you very much!
"Sub count_shapes2()" work for me fine!