Solved

Visio VBA - Switching Layers with a macro

Posted on 2014-01-02
6
2,383 Views
Last Modified: 2014-01-07
I have a macro that switches from a :
technical layer (with IP addresses and port numbers).
management layer (no IP addresses, just icons and host names
CTRL + T for technical
CTRL + M for managerial
Works fine on one sheet.
On page 2, I just created a technical layer, assigned objects and for some reason, the OPPOSITE is taking place.  CTRL + T is showing me the managerial view.  CTRL + M is showing me the tecnical view.  
Same layer names, different page.  Macro works the opposite way!  "T" is now showing managerial view "M" is now showing technical view.
How  can I fix this?
0
Comment
Question by:brothertruffle880
  • 3
  • 2
6 Comments
 
LVL 11

Expert Comment

by:Visio_Guy
ID: 39753594
Hi BT,

The layers on the new page might have been created in a different order than on the first page (look at the ShapeSheet for each page). I suspect you used a macro-recording from the first page, which needs a bit of parametrization.

It's best to find a layer by name, rather than depend on some sort of index. Here's some test code that uses a GetLayerByName function to get a Visio layer object:

Public Sub Test()

  Dim lyr As Visio.Layer
  Set lyr = GetLayerByName(Visio.ActivePage, "Connector")
  
  If (Not (lyr Is Nothing)) Then
  
    '// Do something with the layer:
    Debug.Print lyr.Name
  
    '// Example: add a shape to the layer:
    '// lyr.Add (shp)
    
  End If

End Sub
Function GetLayerByName(ByRef pg As Visio.Page, _
                        ByVal sLayerName As String) As Visio.Layer

  Set GetLayerByName = Nothing
  
  Dim lyr As Visio.Layer
  For Each lyr In pg.Layers
    If (StrComp(sLayerName, lyr, vbTextCompare) = 0) Then
      Set GetLayerByName = lyr
      Exit For
    End If
  Next lyr
  
  '// Cleanup:
  Set lyr = Nothing
End Function

Open in new window

0
 

Author Comment

by:brothertruffle880
ID: 39754891
Hi Visio Guy:
YES!  
You nailed it.  You precisely stated exactly what I did.
Actually, I created three layers in a different order on some pages.

Can you help me fix it.  I'm not a VBA guru so I'm not sure what your code above does.

I have about 30 pages.  There are actually three layers:  management, technical and logistics.  I would like to set up macros so that a user presses CTRL + T to see the technical layer, CTRL + M to see the management layer and CTRL + L to see the logistics layer.
0
 
LVL 11

Expert Comment

by:Visio_Guy
ID: 39759244
Hi BT,

Do you need to assign shapes to layers, or is that already done?

Ie: do you simply need to flip the visibility of the layers on each page according to the names/keyboard shortcuts?
0
 
LVL 11

Accepted Solution

by:
Visio_Guy earned 500 total points
ID: 39761768
Ok, I've updated the VBA to use my snippet from above to switch between the "Tech" and "Management" layers using Ctrl + T or Ctrl + M.
Layer-Switching.vsdFor those interested, below is the VBA code that makes this work, no matter the order of the layers.

Ideally, this code should be placed in a stencil, and that stencil should be open in order for the code to work. When the code is in the document, you will end up with many copies of the code--one for each document based on this one--which can be troublesome.
Option Explicit

Public Const LayerName_Mgmt$ = "Management"
Public Const LayerName_Tech$ = "Tech"

'// ----- Public Procedures ---------------------------------------------------
Public Sub ShowManagementLayer()

  '// Ctrl + M

  Dim pg As Visio.Page
  Set pg = Visio.ActivePage
  
  Call m_showAndHideLayers(pg, LayerName_Mgmt$)
  
  Set pg = Nothing

End Sub
Public Sub ShowTechLayer()

  '// Ctrl + T

  Dim pg As Visio.Page
  Set pg = Visio.ActivePage
  
  Call m_showAndHideLayers(pg, LayerName_Tech$)
  
  Set pg = Nothing

End Sub

'// ----- Private Procedures --------------------------------------------------
Private Sub m_showAndHideLayers(ByRef visPg As Visio.Page, _
                                ByVal sLayerNameToShow As String)
  
  If (visPg.Layers.Count = 0) Then
    Call MsgBox("The active page has no layers!" & vbCrLf & vbCrLf & _
            "No changes to layers will be made.")
    GoTo Cleanup
  End If
  
  '// Try and get the target layer:
  Dim lyrTarget As Visio.Layer, lyr As Visio.Layer
  Set lyrTarget = m_getLayerByName(visPg, sLayerNameToShow)
    
  If (lyrTarget Is Nothing) Then
    '// Layer not found, so don't do anything at all
    Call MsgBox("Layer: '" & sLayerNameToShow & "' was not found!" & vbCrLf & vbCrLf & _
                "No changes to layers will be made.")
    GoTo Cleanup
  Else
    '// The layer was found, turn it visible and active:
    lyrTarget.CellsC(Visio.VisCellIndices.visLayerVisible).ResultIU = 1
    lyrTarget.CellsC(Visio.VisCellIndices.visLayerActive).ResultIU = 1
    
    '// Turn all the other layers invisible and inactive:
    Dim i As Integer
    For i = 1 To visPg.Layers.Count
    
      Set lyr = visPg.Layers.Item(i)
      If (Not (lyr Is lyrTarget)) Then
        lyr.CellsC(Visio.VisCellIndices.visLayerVisible).ResultIU = 0
        lyr.CellsC(Visio.VisCellIndices.visLayerActive).ResultIU = 0
      End If
      
    Next i
  
  End If

  
Cleanup:
    
  '// Cleanup:
  Set lyrTarget = Nothing
  Set lyr = Nothing

End Sub

Private Function m_getLayerByName(ByRef pg As Visio.Page, _
                                  ByVal sLayerName As String) As Visio.Layer

  Set m_getLayerByName = Nothing
  
  Dim lyr As Visio.Layer
  For Each lyr In pg.Layers
    If (StrComp(sLayerName, lyr, vbTextCompare) = 0) Then
      Set m_getLayerByName = lyr
      Exit For
    End If
  Next lyr
  
  '// Cleanup:
  Set lyr = Nothing
  
End Function

Open in new window

0
 

Author Closing Comment

by:brothertruffle880
ID: 39762037
You are amazing!  Thanks!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The ability to add structure to Visio diagrams using containers, lists and callouts is one of my favorite features in Visio 2010. In this article we’ll examine lists. We’ll explore containers and callouts in separate articles. Prior to reading th…
This is pretty cool.  The purpose of this VB Script is to help you document where JAR (Java ARchive) files and specifically java class files are located so that you can address issues seen with a client or that you can speak intelligently with a dev…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now