Visio Guy

Visio Discussions => Programming & Code => Topic started by: droper1 on October 11, 2022, 06:26:53 PM

Title: Macro to Show/Hide a layer across different pages
Post by: droper1 on October 11, 2022, 06: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
Title: Re: Macro to Show/Hide a layer across different pages
Post by: Paul Herber on October 11, 2022, 06:48:32 PM
Try:

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

But this does assume that you want layer number 7 on each page.
Title: Re: Macro to Show/Hide a layer across different pages
Post by: droper1 on October 11, 2022, 07: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.
Title: Re: Macro to Show/Hide a layer across different pages
Post by: wapperdude on October 12, 2022, 02:02:59 AM
See this link...https://learn.microsoft.com/en-us/office/vba/api/visio.layer.name (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.
Title: Re: Macro to Show/Hide a layer across different pages
Post by: hidden layer on October 12, 2022, 06: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:
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
Title: Re: Macro to Show/Hide a layer across different pages
Post by: Surrogate on October 12, 2022, 09:43:35 AM
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 (http://visguy.com/vgforum/index.php?topic=4634.0)...
Title: Re: Macro to Show/Hide a layer across different pages
Post by: hidden layer on October 12, 2022, 11: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.

Title: Re: Macro to Show/Hide a layer across different pages
Post by: 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.
Title: Re: Macro to Show/Hide a layer across different pages
Post by: hidden layer on October 12, 2022, 02:28:14 PM
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!
Title: Re: Macro to Show/Hide a layer across different pages
Post by: droper1 on October 20, 2022, 05: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.
Title: Re: Macro to Show/Hide a layer across different pages
Post by: Paul Herber on October 20, 2022, 05: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.
Title: Re: Macro to Show/Hide a layer across different pages
Post by: droper1 on October 20, 2022, 06: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
Title: Re: Macro to Show/Hide a layer across different pages
Post by: Paul Herber on October 20, 2022, 08: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