Solved

Visio 2013 VBA - Switching layers with a macro - Turning PRINT layer ON and OFF

Posted on 2014-01-14
6
2,271 Views
Last Modified: 2014-01-15
I have three layers, Management, Technical and Voice.
When I switch views from Management to Technical to Voice,  I would ALSO like to be able to print that layer as well.  Currently when I switch layers.  I can see the current layer but I print some other layer.
Here is the code:
Option Explicit

Public Const LayerName_Mgmt$ = "Management"
Public Const LayerName_Tech$ = "Technical"
Public Const LayerName_Voice$ = "Voice"

Sub Management()
' Keyboard Shortcut: Ctrl+m
  '// Ctrl + M

  Dim pg As Visio.Page
  Set pg = Visio.ActivePage
  
  Call m_showAndHideLayers(pg, LayerName_Mgmt$)
  
  Set pg = Nothing
End Sub
Sub Switch_to_voice()
' switches to voice view
    'Enable diagram services
  Dim pg As Visio.Page
  Set pg = Visio.ActivePage
  
  Call m_showAndHideLayers(pg, LayerName_Voice$)
  
  Set pg = Nothing

End Sub
Sub Switch_to_technical()
' switches to technical view
' Keyboard Shortcut: Ctrl+t
    'Enable diagram services
  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
Comment
Question by:brothertruffle880
  • 3
  • 2
6 Comments
 
LVL 3

Expert Comment

by:fredvr666
ID: 39779636
This will work:
lyrTarget.CellsC(visLayerPrint).FormulaU = 1
0
 
LVL 11

Accepted Solution

by:
Visio_Guy earned 500 total points
ID: 39781538
So right beneath these lines:

  lyr.CellsC(Visio.VisCellIndices.visLayerVisible).ResultIU = 0
  lyr.CellsC(Visio.VisCellIndices.visLayerActive).ResultIU = 0

Open in new window

add this line:

  lyr.CellsC(Visio.VisCellIndices.visLayerPrint).ResultIU = 1

Open in new window

0
 

Author Closing Comment

by:brothertruffle880
ID: 39781665
Thank you Visio guy!
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

Author Comment

by:brothertruffle880
ID: 39782174
Thank you Visio Guy.  Your instruction was flawlessly lucid.  I was able to do it even in my early-morning stupor.
0
 
LVL 11

Expert Comment

by:Visio_Guy
ID: 39782195
Too funny!
0
 

Author Comment

by:brothertruffle880
ID: 39783443
Oops.
Found and fixed a minor hitch.  Needed one more line of code.
    '// The layer was found, turn it visible and active:
            lyrTarget.CellsC(Visio.VisCellIndices.visLayerVisible).ResultIU = 1
            lyrTarget.CellsC(Visio.VisCellIndices.visLayerActive).ResultIU = 1
            lyrTarget.CellsC(Visio.VisCellIndices.visLayerPrint).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
                        lyr.CellsC(Visio.VisCellIndices.visLayerPrint).ResultIU = 0

Open in new window

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

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…
The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…

911 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

18 Experts available now in Live!

Get 1:1 Help Now