For Each does not find all shapes / deleting shapes(!)

Started by hidden layer, April 04, 2023, 09:34:48 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

hidden layer

Hi there,
To delete some shapes I started with the following code:
For Each shp In ActivePage.Shapes
Debug.Print shp.ID & " " & shp.Cells("Comment").FormulaU
    cmt = shp.Cells("Comment").FormulaU
    If cmt = tGtK Then
    Debug.Print shp.ID & " x"
        shp.Delete
    End If
Next shp

The funny thing is that if there are 6 shapes that matches the criteria only 3 will be deleted.
As a workaround I copied this code 4 times in the sub - then all shapes (that matches the criteria) will be deleted. Imagine I'd have 100 matches to delete...
The immediate Window shows that the For-loop starts at the highest number:
586 "Kringl"
586 x
584 "Kringl"
584 x
582 "Kringl"
582 x
1 Prop.Comment
6 ""
7 ""
13 "ABC"
21 ""
and so on

The shapes that matches but not have been deleted are 581, 583 and 585. They are not in the list.

What did I miss?


Thanks!
hl
'//edit subject\\

Paul Herber

When you are deleting shapes you need to count backwards through the shapes, i.e. start at

For counter = ActivePage.Shapes.Count To 1 Step -1
  shp = ActivePage.Shapes(counter)



http://www.visguy.com/2015/07/07/deleting-visio-shapes-programmatically/
Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

hidden layer

Hi Paul,
that works!

I had to change the 2nd line into Set shp = ActivePage.Shapes(counter) and it works like a charm.

good to know ;)

Thanks!

Paul Herber

Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/

Surrogate


Paul Herber

I used to be a Delphi man, back in the days when it was a Borland product, but to use it with Visio I had to use a framework that was 32-bit only, and it also gave problems with UAC under Windows 7/8.
Then Enbarcadero took over.
I then moved to Lazarus for some stuff, but decided C# was the way forward for my Visio addons.

Electronic and Electrical engineering, business and software stencils for Visio -

https://www.paulherber.co.uk/