Visio Guy

Visio Discussions => Programming & Code => Topic started by: tepebox on August 30, 2021, 08:04:38 PM

Title: Deleting Layers
Post by: tepebox on August 30, 2021, 08:04:38 PM
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
Title: Re: Deleting Layers
Post by: Paul Herber on August 30, 2021, 08:10:14 PM
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.
Title: Re: Deleting Layers
Post by: tepebox on August 30, 2021, 08:32:16 PM
THANKS so much!!!


Thomas