Solved

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

Posted on 2014-01-14
6
2,377 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
[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 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

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

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

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…
Deploying a Microsoft Access application in a Citrix environment is not difficult but takes a few steps. However, Citrix system people are often of little help, as they typically know next to nothing about Access. The script provided here will take …
This is a high-level webinar that covers the history of enterprise open source database use. It addresses both the advantages companies see in using open source database technologies, as well as the fears and reservations they might have. In this…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…

728 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