Ok, I must be losing memory or something, but what is the correct way to loop through a ShapeRange that changes over time? As an example I have something like this (in pseudocode):
Tag:For Each S In SR.Shapes.All For Each T In SR.Shapes.All If S compared to T is something Then T.Delete Goto Tag End If Next T Next S
Tag:For Each S In SR.Shapes.All
For Each T In SR.Shapes.All
If S compared to T is something Then
T.Delete Goto Tag
And no matter what I try:
it throws an error at some point that the shapes are no longer there. Basically what I want to do here is remove overlapping shapes in the same ShapeRange with specific parameters.
Please, try the next way:
Dim SR As ShapeRange, shDel As New ShapeRange, s As Shape, T As Shape
Set SR = ActiveLayer.Shapes.All
For Each s In SR.Shapes
For Each T In SR.Shapes
If SR.IndexOf(s) <> SR.IndexOf(T) Then 'to avoid testing of the same shape overlapping
If s.DisplayCurve.IntersectsWith(T.DisplayCurve) Then
If shDel.count > 0 Then 'if there are shapes in the shape range to be deleted
If shDel.IndexOf(T) = 0 Then shDel.Add s 'it checks if the shape already exists before adding it
shDel.Add s 'add the shape in the shape range to be deleted
shDel.CreateSelection 'selecting only for testing
If it selects what you need, it is enough to replace `CreateSelection` with `Delete`...
no need to use SR.Shapes.All or SR.Shapes (For Each s In sr)to check if shape exists in the document you can set error handling (On Error Goto ...) and try to move it (s.Move 0.0)try to use less Goto, use Exit For insteadyou can also use loops with counters, for example:
For z = 1 To sr.Count - 1 For i = z + 1 To sr.Count 'use next shape and all subsequent ones If sr(z) =compare= sr(i) then Exit For 'goto after Next i Next iNext z
So far this works incredibly well. I wasn't aware of IndexOf, but it really helps for something like this. Thanks a lot for the detailed code and comments!
Thanks for the comment. I will try to merge elements from both replies. Also, this should help with some other functions I have been making.