You are correct, using an error handler is ... bad style. And for a long script, it might make things run slower.
Your problem is one that pops up when doing a number of actions in InDesign. The program updates the PageItems and Graphs counts on each edit, and that means the for ..next loops are messed up. There is, fortunately, a standard work-around this: loop backwards instead!
for elemento = app.activeDocument.Pages(2).PageItems.count to 1 step -1
(I think I got the VB syntax right.) In effect, the page items are still updated when you delete one, but it doesn't matter anymore as the only ones that wil be effected are already processed.
[Note] You might want to try your other idea again -- using allGraphics directly should be much faster. You can "go" from an allGraphics array element -- a Graphic object -- to its container, simply by using the allGraphics parent property. That always points directly to the frame the image is in. Apart from any VB syntax errors, something like this ought to work:
Looping backwards over the AllGraphics array, of course. Just remember always to do that when you are changing stuff.
Thank you... very creative solutions... I will test both in my script and will post back... thank you!!!
The sole solution
work ONLY removing the parent graphic... but the error still... I tried to invert the loop the way you said in conjunction with the first one and seems to be working fine... thanks for your colaboration!!!