Referencing layers by name instead of ID

Started by anico, November 27, 2018, 01:40:28 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

anico

Hi Visio gurus,

I'm having trouble. I know this is far from "kosher," but all I want to do is directly reference layers by name. For example I have several layers, two of which are "ClmStatInquiry" and "ClmStatOff" Right now, I'm using IF statements to dictate what layer shapes belong to. The problem is for some reason the IDs I'm using aren't uniform? I read that it's something to do with the active page and layer indexing not working.

I recorded macros of me changing which layer a shape belongs to and used the following line to change which layer the shape ClmStat belongs to:
Application.ActiveDocument.Pages.ItemU("Non-Hosted").Shapes("ClmStat").CellsSRC(visSectionObject, visRowLayerMem, visLayerMember).FormulaForceU = """10"""
And I simply change the """10""" at the end to reflect that of whichever layer index I want ClmStat to be a part of. The problem is using this line is adding it to a layer with a totally different index!
Through recording macros to grab the layer indexes, I know for a fact that "ClmStatInquiry" is layer 10, and "ClmStatOff" is layer 11, yet referring to layer 10 as """10""" puts a shape on layer 11.

Essentially all I want to do is change what layers certain shapes belong to with a slough of IF statements. I've done some digging in the forum and haven't been able to find a simple way to refer to a layer by name rather than ID. I could really use some help here.

wapperdude

Layers are tricky.  For one, you can't use names directly.  For two, the order from page to page isn't necessarily consistent.

Basically, you need to search each page to find the layer number that has the same name as desired.  Then use that number to assign as your shape layer.

See the following macro:

Sub macro1()
'Select a shape to be assigned to desired layer before running macro.

    Dim vsoLyr As Visio.Layer
    Dim LyrCnt As Integer
    Dim vsoShp as shape

    Set vsoShp = ActiveWindow.Selection(1)    'Assumes only desired shape is selected
   
    LyrCnt = ActivePage.Layers.Count
    For i = 1 To LyrCnt
        LyrName = ActivePage.Layers(i)
        Debug.Print LyrName
        If LyrName = "Dummy" Then   'Enter desired layer name here, instead of Dummy
            vsoShp.CellsSRC(visSectionObject, visRowLayerMem, visLayerMember).FormulaForceU = Chr(34) & i & Chr(34)    'use Chr(34) instead of bazillion quotes
        End If
    Next
End Sub


Wapperdude
Visio 2019 Pro

metuemre

You can reference a layer by its name directly if I'm not mistaken. It would be like Set vsoLayer = ActivePage.Layers.Item("Dummy") 'Enter desired layer name here, instead of Dummy Then you can use Layer.Add method to add shapes to a specific layer. More info can be found on
https://docs.microsoft.com/en-us/office/vba/api/visio.layers.item
https://docs.microsoft.com/en-us/office/vba/api/visio.layer.add

wapperdude

Ah.  New visio stuff.  Metuemre is correct.  Looks like this was added, beginning with Visio 2013, if I'm not mistaken.  The future has solutions to this oversight.

Beware of the direct layer naming approach.  It works fine as long as the page has the layer named.  Creates an error if not.  So, error catching might be necessary.

Wapperdude
Visio 2019 Pro

anico