Macro to Show/Hide a layer across different pages

Started by droper1, October 11, 2022, 06:26:53 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

droper1

I've got two macros that can show or hide a certain layer on a page, but it doesn't want to work on different pages in the same file. The Debug is highlighting the Item # as the issue. I've renamed the layer "1-Valve Lineup" so that it's the top layer on all pages, but that doesn't fix the issue.

Sub HideVL()

    'Enable diagram services
    Dim DiagramServices As Integer
    DiagramServices = ActiveDocument.DiagramServicesEnabled
    ActiveDocument.DiagramServicesEnabled = visServiceVersion140 + visServiceVersion150

    Dim UndoScopeID1 As Long
    UndoScopeID1 = Application.BeginUndoScope("Layer Properties")
    Dim vsoLayer1 As Visio.Layer
    Set vsoLayer1 = Application.ActiveWindow.Page.Layers.Item(7)
    vsoLayer1.CellsC(visLayerVisible).FormulaU = "0"
    vsoLayer1.CellsC(visLayerPrint).FormulaU = "0"
    Application.EndUndoScope UndoScopeID1, True

    'Restore diagram services
    ActiveDocument.DiagramServicesEnabled = DiagramServices

Paul Herber

Try:

Set vsoLayer1 = Application.ActivePage.Layers.Item(7)

But this does assume that you want layer number 7 on each page.
Electronic and Electrical engineering, business and software stencils for Visio -

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

droper1

#2
Thanks! Is there any way to call out a specific layer by name? I'm getting error messages on the other pages despite naming the layer 1-LayerName so that it is the first in the list on all pages.

wapperdude

See this link...https://learn.microsoft.com/en-us/office/vba/api/visio.layer.name

Note that Visio doesn't arrange the layer order based upon name.  It is based upon order of definition.  I believe duplicating a page might preserve layer names and the order.  But I could be wrong.
Visio 2019 Pro

hidden layer

Hi droper,

wapperdude is right. You have to call layers by name.
I made a userform that have checkboxes for each layer and it works like this:
For Each CeBe In Me.Controls
    layerName = CeBe.name
        For pages = 1 To ActiveDocument.pages.Count
                With Application.ActiveDocument.pages(pages)
                    For layers = 1 To Application.ActiveDocument.pages(pages).layers.Count
                        If .layers(layers).name = layerName Then
                            If CeBe.Value = True Then
                                Application.ActiveDocument.pages(pages).layers.Item(layers).CellsC(visLayerVisible).FormulaU = "1"
                            Else
                                Application.ActiveDocument.pages(pages).layers.Item(layers).CellsC(visLayerVisible).FormulaU = "0"
                            End If
                        End If
                    Next layers
                End With
        Next pages
Next CeBe


cheers
hl

Surrogate

Quote from: wapperdude on October 12, 2022, 02:02:59 AMNote that Visio doesn't arrange the layer order based upon name.  It is based upon order of definition.  I believe duplicating a page might preserve layer names and the order.  But I could be wrong.
This nuance can lead to inconsistencies
Quote from: Surrogate on May 17, 2022, 02:42:55 PM
Another point that complicates things a bit.
The layer numbers in the Page's PageSheet start with 1.
And for the shapes in the LayerMembership cell, the layer numbers start with 0.
I recommend read this thread Assign to Layer" list...

hidden layer

#6
If you have too many (?) layers the userform gets quiet big.
The document I use daily have about 100 commandbuttons - one for each layer.


vojo

could use this show/hide vba to only show buttons specific to that layer.  Ie context buttons.

hidden layer

Quote from: vojo on October 12, 2022, 01:32:23 PM
could use this show/hide vba to only show buttons specific to that layer.  Ie context buttons.

This is a question?

The Commandbuttons I use are one for each layer. The CB's color is dependent of the layer's visibility before opening the UF.
Of course you can toggle the CB's visibility but I don't have this necessity.

hl

btw: Thanks Paul!

droper1

So this is the code I managed to fat finger through that works for individual pages across a document.

Sub Test()

'test macro for Unhiding a layer
Set vsoLayer1 = Application.ActiveWindow.Page.Layers.Item("Valve Lineup")
vsoLayer1.CellsC(visLayerVisible).FormulaU = "1"
vsoLayer1.CellsC(visLayerPrint).FormulaU = "1"
   
End Sub

I can't seem to figure out how to have it execute across all pages of a document at the same time. I've tried adding this bit that I found in another QA forum:

Dim pg as Page
For Each pg in Activedocument.Pages
      Next

But this doesn't seem to do anything. Doesn't break the code, so the layer will unhide on my current page, but no others in the document. I'd also like to be able to Hide or Show a layer using one macro based on its current state, but that's well beyond me currently.

Paul Herber

Get rid of the line:
Set vsoLayer1 = Application.ActiveWindow.Page.Layers.Item("Valve Lineup")
and put
Set vsoLayer1 = ActivePage.Layers.Item("Valve Lineup")
inside that loop, then your other code.
Electronic and Electrical engineering, business and software stencils for Visio -

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

droper1

I've tried this bit in a handful of different configurations, but the "For Each pg In ActiveDocument.Pages" bit doesn't seem to be doing anything to any page but the active one.

Sub Test()
'test macro for Unhiding a layer
Dim pg As Page
For Each pg In ActiveDocument.Pages
    Set vsoLayer1 = ActivePage.Layers.Item("Valve Lineup")
    vsoLayer1.CellsC(visLayerVisible).FormulaU = "1"
    vsoLayer1.CellsC(visLayerPrint).FormulaU = "1"
Next
   
End Sub

Paul Herber

Sorry, setting pg in the loop doesn't make it the active page:

For Each pg In ActiveDocument.Pages
    Set vsoLayer1 = ActiveDocument.Pages[pg].Layers.Item("Valve Lineup")
    vsoLayer1.CellsC(visLayerVisible).FormulaU = "1"
    vsoLayer1.CellsC(visLayerPrint).FormulaU = "1"
Next
Electronic and Electrical engineering, business and software stencils for Visio -

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