?
Solved

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

Posted on 2014-01-14
6
Medium Priority
?
2,419 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 2000 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
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 

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

Independent Software Vendors: 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!

Question has a verified solution.

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

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…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses

800 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