Link to home
Start Free TrialLog in
Avatar of brothertruffle880
brothertruffle880Flag for United States of America

asked on

Visio VBA - Switching Layers with a macro

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?
Avatar of Chris Roth
Chris Roth
Flag of Germany image

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

Avatar of brothertruffle880

ASKER

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.
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?
ASKER CERTIFIED SOLUTION
Avatar of Chris Roth
Chris Roth
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You are amazing!  Thanks!