Delete Layers (And shapes on them) that are marked as "Not Visible"

Started by Biomedmike, January 29, 2025, 10:08:07 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Biomedmike

Well it didn't take me long to be stumped.

I am trying to delete layers (and all the shapes on them) on a page if the Layer is hidden.

I use the code below but get a Run Time Error "438":
    Object doesn't support this property or method.

What could I be doing worng?

Sub deletelayers()

Dim vsoShape As Visio.Shape
    Dim vsoLayer As Visio.Layer
    For Each vsoLayer In ActiveDocument.Layers  ' This is the line that errors
        If vsoLayer.visible = False Then
            vsoLayer.Delete (True)
        End If
    Next vsoLayer
   
End Sub

(Stumped Again) BiomedMike

Thomas Winkel

1. A document has no layers, but a page has.
2. "visible" is not a property of a layer.

Sub DeleteLayers()
    Dim vsoShape As Visio.Shape
    Dim vsoLayer As Visio.Layer
   
    For Each vsoLayer In ActivePage.Layers
        If Not CBool(vsoLayer.CellsC(visLayerVisible)) Then
            vsoLayer.Delete True
        End If
    Next vsoLayer
End Sub

Nikolay

The Thomas' code looks great, just two cents - you usually need to delete things in reverse order in Visio.
It is not safeguarded against that. If you are deleting things in the same collection you are iterating over (using For Each), bad things may happen.
I would do this:

    Dim vsoLayer As Visio.Layer
    For i = ActivePage.Layers.Count To 1 Step -1
        Set vsoLayer = ActivePage.Layers(i)
        If Not CBool(vsoLayer.CellsC(visLayerVisible)) Then
            vsoLayer.Delete (True)
        End If
    Next

I mean, for example, this code WILL NOT delete all shapes on the page,
like one could expect, but only half of them:

For Each shp in ActivePage.Shapes
  shp.Delete
Next

And in some cases (with some other collections), it could get worse

Thomas Winkel

Good point. Even today I still fall for it almost every time 🙈
Sometimes I do it in two steps:
1. Collect items in Dictionaries in a for each loop
2. Finally loop over the dictionary and delete the items

Nikolay

I think this is totally a fault of Visio API developers.
The API can (and should) be properly protected from stuff like this.

Paul Herber

Just to throw a spanner in the works (something I am well qualified to do) you might need to take into account the fact that shapes can be on more than one layer, one (or more) visible, one (or more) invisible. Also grouped shapes can contain sub-shapes that are also independently layered. Your particular application might not need this but for a general purpose routine you would need to take this into account. My 2p.

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

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

Biomedmike

AMAZING! THANK YOU ALL!

This works PERFECTLY!

I continue to be amazed at the knowledge of you all.

Thank you,

BiomedMike

Browser ID: smf (possibly_robot)
Templates: 4: index (default), Display (default), GenericControls (default), GenericControls (default).
Sub templates: 6: init, html_above, body_above, main, body_below, html_below.
Language files: 4: index+Modifications.english (default), Post.english (default), Editor.english (default), Drafts.english (default).
Style sheets: 4: index.css, attachments.css, jquery.sceditor.css, responsive.css.
Hooks called: 260 (show)
Files included: 34 - 1306KB. (show)
Memory used: 1154KB.
Tokens: post-login.
Cache hits: 13: 0.00112s for 26,597 bytes (show)
Cache misses: 2: (show)
Queries used: 19.

[Show Queries]