In a previous article, we showed you how to take advantage of Visio Layers to display different type of information (L1, L2/L3, routing) on a network diagram without overloading it. In this article we propose you a shortcut to quickly hide or display a particular layer.
Software used in this scenario : Microsoft Visio 2013 (15.0.4420.107) MSO (15.0.4420.107) 64 bits
The idea is to add a switch button for each layer to hide or display this one :

We didn’t find a simple way to add a button on the Visio sheet (as it’s possible with Excel) so we decided to take advantage of the « Double-click » action to run a macro when user double-click on a rectange shape.
Step 1 : create macros
Enable the « Developper » tab which is not displayed in the ribbon by default (click right on the ribbon) :
In the « Developper » tab, create a new macro by clicking on « Record Macro » :

Give a name to your macro and press OK :
You notice changes in the code section in the « Developper » tab. « Pause Recording » is now available and « Record Macro » button change to « Stop Recording ». These changes indicate that you are in recording mode.
Return in the « Home » tab and, open the « Layer Properties » box and uncheck the « Visible » box for the layer « routing » :
Stop macro recording and open the « Macros » dialog box :

Select the newly created macro and click « Edit » :

It open the Visual Basic Editor :
In the code editor window, select lines below :
And replace them with this code :
Dim vsoLayer1 As Visio.Layer Set vsoLayer1 = Application.ActiveWindow.Page.Layers.Item("none") If vsoLayer1.CellsC(visLayerVisible) = 0 Then vsoLayer1.CellsC(visLayerVisible).FormulaU = 1 ElseIf vsoLayer1.CellsC(visLayerVisible) = 1 Then vsoLayer1.CellsC(visLayerVisible).FormulaU = 0 End If If vsoLayer1.CellsC(visLayerPrint) = 0 Then vsoLayer1.CellsC(visLayerPrint).FormulaU = 1 ElseIf vsoLayer1.CellsC(visLayerPrint) = 1 Then vsoLayer1.CellsC(visLayerPrint).FormulaU = 0 End If
|
Because the goal of this article is not to teach you about VBA coding, we don’t explain you in detail the purpose of each line.

In short, red blocks below are best practice code automatically added when recording macro (see
https://social.technet.microsoft.com/Forums/office/en-US/3b7e95a7-bc8f-4fef-b604-7a6dc7898bf5/diagram-services?forum=visiogeneral) :
The rest of the code is the switching function to set or unset the layer visible and printable and is inspired by the code that has been automatically generated with the recording tool.
Duplicate the sub « none » for each layer of your network diagram and change sub name and « Application.ActiveWindow.Page.Layers.Item » selection appropriately :
Sub L1()
'
' Macro to quickly hide or display the "L1" layer
'
'Enable diagram services
Dim DiagramServices As Integer
DiagramServices = ActiveDocument.DiagramServicesEnabled
ActiveDocument.DiagramServicesEnabled = visServiceVersion140 + visServiceVersion150
Dim vsoLayer1 As Visio.Layer
Set vsoLayer1 = Application.ActiveWindow.Page.Layers.Item("L1")
If vsoLayer1.CellsC(visLayerVisible) = 0 Then
vsoLayer1.CellsC(visLayerVisible).FormulaU = 1
ElseIf vsoLayer1.CellsC(visLayerVisible) = 1 Then
vsoLayer1.CellsC(visLayerVisible).FormulaU = 0
End If
If vsoLayer1.CellsC(visLayerPrint) = 0 Then
vsoLayer1.CellsC(visLayerPrint).FormulaU = 1
ElseIf vsoLayer1.CellsC(visLayerPrint) = 1 Then
vsoLayer1.CellsC(visLayerPrint).FormulaU = 0
End If
'Restore diagram services
ActiveDocument.DiagramServicesEnabled = DiagramServices
End Sub
Sub L2L3()
'
' Macro to quickly hide or display the "L2L3" layer
'
'Enable diagram services
Dim DiagramServices As Integer
DiagramServices = ActiveDocument.DiagramServicesEnabled
ActiveDocument.DiagramServicesEnabled = visServiceVersion140 + visServiceVersion150
Dim vsoLayer1 As Visio.Layer
Set vsoLayer1 = Application.ActiveWindow.Page.Layers.Item("L2L3")
If vsoLayer1.CellsC(visLayerVisible) = 0 Then
vsoLayer1.CellsC(visLayerVisible).FormulaU = 1
ElseIf vsoLayer1.CellsC(visLayerVisible) = 1 Then
vsoLayer1.CellsC(visLayerVisible).FormulaU = 0
End If
If vsoLayer1.CellsC(visLayerPrint) = 0 Then
vsoLayer1.CellsC(visLayerPrint).FormulaU = 1
ElseIf vsoLayer1.CellsC(visLayerPrint) = 1 Then
vsoLayer1.CellsC(visLayerPrint).FormulaU = 0
End If
'Restore diagram services
ActiveDocument.DiagramServicesEnabled = DiagramServices
End Sub
Sub routing()
'
' Macro to quickly hide or display the "routing" layer
'
'Enable diagram services
Dim DiagramServices As Integer
DiagramServices = ActiveDocument.DiagramServicesEnabled
ActiveDocument.DiagramServicesEnabled = visServiceVersion140 + visServiceVersion150
Dim vsoLayer1 As Visio.Layer
Set vsoLayer1 = Application.ActiveWindow.Page.Layers.Item("routing")
If vsoLayer1.CellsC(visLayerVisible) = 0 Then
vsoLayer1.CellsC(visLayerVisible).FormulaU = 1
ElseIf vsoLayer1.CellsC(visLayerVisible) = 1 Then
vsoLayer1.CellsC(visLayerVisible).FormulaU = 0
End If
If vsoLayer1.CellsC(visLayerPrint) = 0 Then
vsoLayer1.CellsC(visLayerPrint).FormulaU = 1
ElseIf vsoLayer1.CellsC(visLayerPrint) = 1 Then
vsoLayer1.CellsC(visLayerPrint).FormulaU = 0
End If
'Restore diagram services
ActiveDocument.DiagramServicesEnabled = DiagramServices
End Sub
|
Save your code :

Visio may ask you to change the file format to support
macros :

Select No. In the « Save as » dialog box choose
« Visio macro-enabled drawing » file format :

This lead to the creation of a .vsdm file (format which
support macros).
At the end of this step, you should see your 4 macros :

Step 2 : create craft buttons
Instert 4 rectangle shapes, one for each layer :
Select the first rectangle « L1 » and open the
« Behavior » box in the « Developper » tab. Uncheck all
options under the « Selection Highlighting » section in the
« Behavior » tab :

In the « Double-Click » tab activate the
« Run macro » option and select the « L1 » macro :

Now you can display or hide any layer by double-clicking on
the corresponding craft button (it also set up correct configuration to print
what is displayed) :
Do the same for others rectangles.
References
https://support.office.com/en-US/article/Change-a-shape-s-double-click-behavior-29757697-eec0-4e82-b5e4-bf045a4629ae
https://support.office.com/en-us/article/Record-macros-in-Visio-05c29ac5-922d-4fd4-8d60-8e2ccd3ecc5a
https://social.technet.microsoft.com/Forums/office/en-US/3b7e95a7-bc8f-4fef-b604-7a6dc7898bf5/diagram-services?forum=visiogeneral