Author Topic: Macro to Show/Hide a layer across different pages  (Read 825 times)

0 Members and 1 Guest are viewing this topic.

droper1

  • Newbie
  • *
  • Posts: 5
Macro to Show/Hide a layer across different pages
« on: October 11, 2022, 01:26:53 PM »
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3310
    • Paul Herber's website
Re: Macro to Show/Hide a layer across different pages
« Reply #1 on: October 11, 2022, 01:48:32 PM »
Try:
Code
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 and applications for Visio -

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

droper1

  • Newbie
  • *
  • Posts: 5
Re: Macro to Show/Hide a layer across different pages
« Reply #2 on: October 11, 2022, 02:56:25 PM »
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.
« Last Edit: October 11, 2022, 03:08:28 PM by droper1 »

wapperdude

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4477
  • Ideas Visio-lized into solutions
Re: Macro to Show/Hide a layer across different pages
« Reply #3 on: October 11, 2022, 09:02:59 PM »
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

  • Full Member
  • ***
  • Posts: 89
Re: Macro to Show/Hide a layer across different pages
« Reply #4 on: October 12, 2022, 01:21:12 AM »
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:
Code
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

  • Hero Member
  • *****
  • Posts: 1636
    • ShapeSheet™ Knowledge Base
Re: Macro to Show/Hide a layer across different pages
« Reply #5 on: October 12, 2022, 04:43:35 AM »
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.
This nuance can lead to inconsistencies
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

  • Full Member
  • ***
  • Posts: 89
Re: Macro to Show/Hide a layer across different pages
« Reply #6 on: October 12, 2022, 06:17:10 AM »
If you have too many (?) layers the userform gets quiet big.
The document I use daily have about 100 commandbuttons - one for each layer.

« Last Edit: October 12, 2022, 09:19:13 AM by Paul Herber »

vojo

  • Hero Member
  • *****
  • Posts: 1676
Re: Macro to Show/Hide a layer across different pages
« Reply #7 on: October 12, 2022, 08:32:23 AM »
could use this show/hide vba to only show buttons specific to that layer.  Ie context buttons.

hidden layer

  • Full Member
  • ***
  • Posts: 89
Re: Macro to Show/Hide a layer across different pages
« Reply #8 on: October 12, 2022, 09:28:14 AM »
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

  • Newbie
  • *
  • Posts: 5
Re: Macro to Show/Hide a layer across different pages
« Reply #9 on: October 20, 2022, 12:17:01 PM »
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3310
    • Paul Herber's website
Re: Macro to Show/Hide a layer across different pages
« Reply #10 on: October 20, 2022, 12:37:25 PM »
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 and applications for Visio -

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

droper1

  • Newbie
  • *
  • Posts: 5
Re: Macro to Show/Hide a layer across different pages
« Reply #11 on: October 20, 2022, 01:31:41 PM »
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

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 3310
    • Paul Herber's website
Re: Macro to Show/Hide a layer across different pages
« Reply #12 on: October 20, 2022, 03:21:53 PM »
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 and applications for Visio -

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