Deleting Layers

Started by tepebox, August 30, 2021, 08:04:38 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

tepebox

Hi,

any idea what's wrong with this code (which should delete all layers which have "<>" in it):

    Set Layers = Visio.ActivePage.Layers
    For Each layer In Layers
        If InStr(layer.Name, "<>") > 0 Then
            layer.Delete (0)
        End If
    Next

What happens is that only a few of the layers are delete without any error. I can run this code again and again and always are a few layers removed and at some point in time all are gone... I bet this code already worked a while ago but now it doesn't...

Any idea?

Thanks,


Thomas

Paul Herber

If, say, layer1 gets deleted then what was layer2 then becomes layer1. But the looping doesn't know that so moves on to the new layer2, missing out what is now layer1.
You need to use a normal FOR loop but work backwards through the count.
Electronic and Electrical engineering, business and software stencils for Visio -

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

tepebox