Deleting Layers - Phantom Layer ShapeSheet Rows

Started by perry59, November 01, 2019, 01:24:58 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

perry59

I know this has been brought up before, but the solution does not seem to work.
A part of my application will delete a named layer, which appears to work until you look at the pages shapesheet and see nameless layers left behind. I can't find any way to get rid of those empty layers. In fact, I can't even seem to get the cell representing the layers name!
Can anyone help me with this?

Thanks

here is the vb.net code I am using...

For Each visPage In visDoc.Pages
                For Each visLayer In visPage.Layers
                    If visLayer.Name = LayerName Then
                        visShape = visPage.PageSheet
                        If visShape.CellExists(LayerName, True) Then 'dont work
                            visRowIndex = visShape.CellsRowIndex(LayerName)
                            visShape.DeleteRow(CShort(Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionLayer), visRowIndex)
                        End If
                        'visLayer.CellsC(Visio.VisCellIndices.visLayerLock).FormulaU = "0" 'this does not help
                        visLayer.Delete(False) 'deletes layer but leaves "empty" row in layers table
                        End If
                Next visLayer
            Next visPage

what, me worry?

wapperdude

#1
Nasty problem...
  (a) you have to ask for number of rows in the Layer section, not number of layers.  Layer count omits rows with name ""
  (b) because you're deleting rows, you have to work from highest count to lowest.  Each time a row is deleted, Visio immediately re-indexes.
  (c) the rows are "0" based, not "1" based.  So first row is row =0, not 1.

...but here's solution in VBA:


Sub Macro1()
   
    Dim LyrNullName As String
    Dim intRows As Integer
    Dim vShp As Visio.Shape
   
    Windows.ItemEx("Drawing1:Page-1 <PAGE>").Activate
   
    LyrNullName = ""
    Set vShp = ActiveWindow.Shape
    intRows = vShp.RowCount(Visio.visSectionLayer)
   
    For i = intRows - 1 To 0 Step -1
        lyrname = vShp.CellsSRC(visSectionLayer, i, 0).ResultStr("")
   
        If StrComp(LyrNullName, lyrname, 1) Then
           Debug.Print "layer number = ", i + 1
        Else
            vShp.DeleteRow visSectionLayer, i
        End If
    Next

End Sub

Visio 2019 Pro

Yacine

#2
I think you would be better off using the built in layer.delete method.

https://docs.microsoft.com/en-us/office/vba/api/visio.layer.delete
Yacine

wapperdude

#3
@Yacine:  normally yes, but in this case, there's no layer info for the "layer" part of layer.delete. 
Visio 2019 Pro

Yacine

#4
OK
Yacine

Visio Guy

You should manipulate layers via the layer methods, e.g.: Visio.ActivePage.Layers(index_or_name)

I noticed that deleting layers, even using layer methods, still leaves blank, unused layer rows in the Page's ShapeSheet. But they don't show in the Layer Properties dialog, so I guess we should just *sigh* and accept it :)

If you add a new layer later, Visio will re-use the "empty" row, so Visio is properly tracking which layer rows are really in use, and which rows aren't.

If you are using layer-manipulation methods, you won't ever care about these rows anyway.


I can imagine a case where you have other cells referring to layer formulas, and this might screw things up. But if you are just using layers for layer's sake, then this is all esoteric.

I was able to select a cell in a "phantom layer" row, and test some SelectedCell properties via automation:

?visio.ActiveWindow.SelectedCell.Name
Layers.Visible[2]

?visio.ActiveWindow.SelectedCell.Row
1

So, ShapeSheet-wise, the row is really there, but there is a disconnect with the Layer Properties dialog that could potentially be dangerous. Any ShapeSheet logic that references Layer cells should be carefully updated when layers are added or deleted.




For articles, tips and free content, see the Visio Guy Website at http://www.visguy.com
Get my Visio Book! Using Microsoft Visio 2010