Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Visio VBA - Switching Layers with a macro

Posted on 2014-01-02
6
Medium Priority
?
2,739 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Periodically someone asks me whether there’s a way to automatically convert all of the pages in a Visio drawing to PowerPoint slides. There have even been a few times when I’ve wanted to do that myself but I never really had enough incentive to figu…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

688 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