Microsoft Office Forums

Go Back   Microsoft Office Forums > Microsoft Office > Visio

Reply
 
LinkBack Thread Tools Display Modes
  #1  
Old 12-03-2018, 04:33 PM
ivano ivano is offline Windows 10 Office 2013
Novice
 
Join Date: Dec 2018
Posts: 3
ivano is on a distinguished road
Default show or hide layers in all pages

Hi,
I have visio 2013 and have many pages with several layers. I have 3 layers that I want to show or hide through checkboxes. In one page I created three checkmark boxes and put together some code that will either hide or show a specific layer when it's corresponding checkmark box is either checked or unchecked. It works only for that one page. I want it to work on all pages in the document. As you can see below, I have it as three separate pieces of code but if there's a better way to combine it into one piece of code please feel free to mash it into one.
My code is:


Public Sub CheckBox1_Click()
Dim LayersObj As Visio.Layers
Dim LayerObj As Visio.Layer
Dim LayerName As String


Dim LayerCellObj As Visio.Cell

Set LayersObj = ActivePage.Layers
For Each LayerObj In LayersObj
LayerName = LayerObj.Name
If LayerName = "To Be Form" Then
Set LayerCellObj = LayerObj.CellsC(visLayerVisible)
If CheckBox1.Value Then
LayerCellObj.Formula = True Or 1
Else
LayerCellObj.Formula = False Or 0
End If
End If
Next
End Sub


Public Sub CheckBox2_Click()
Dim LayersObj As Visio.Layers
Dim LayerObj As Visio.Layer
Dim LayerName As String
Dim LayerCellObj As Visio.Cell

Set LayersObj = ActivePage.Layers
For Each LayerObj In LayersObj
LayerName = LayerObj.Name
If LayerName = "SHC As Is Form" Then
Set LayerCellObj = LayerObj.CellsC(visLayerVisible)
If CheckBox2.Value Then
LayerCellObj.Formula = True Or 1
Else
LayerCellObj.Formula = False Or 0
End If
End If
Next
End Sub



Public Sub CheckBox3_Click()
Dim LayersObj As Visio.Layers
Dim LayerObj As Visio.Layer
Dim LayerName As String
Dim LayerCellObj As Visio.Cell

Set LayersObj = ActivePage.Layers
For Each LayerObj In LayersObj
LayerName = LayerObj.Name
If LayerName = "Works As Is Form" Then
Set LayerCellObj = LayerObj.CellsC(visLayerVisible)
If CheckBox3.Value Then
LayerCellObj.Formula = True Or 1
Else
LayerCellObj.Formula = False Or 0
End If
End If
Next
End Sub



I've attached a sample of the document too.


Thanks
Reply With Quote
  #2  
Old 12-03-2018, 06:28 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 963
Guessed is on a distinguished road
Default

As a concept (ie I didn't test this because I don't know where you got your Checkboxes from) try these macros
Code:
Public Sub CheckBox1_Click()
  ShowHideLayer "To Be Form", CheckBox1.Value
End Sub

Public Sub CheckBox2_Click()
  ShowHideLayer "SHC As Is Form", CheckBox2.Value
End Sub

Public Sub CheckBox3_Click()
  ShowHideLayer "Works As Is Form", CheckBox3.Value
End Sub

Public Sub ShowHideLayer(sLayerName As String, bShow As Boolean)
  Dim aPage As Visio.Page, aLayer As Visio.Layer, aCell As Visio.Cell
  
  For Each aPage In ActiveDocument.Pages
    For Each aLayer In aPage.Layers
      If aLayer.Name = sLayerName Then
        Set aCell = aLayer.CellsC(visLayerVisible)
        aCell.Formula = bShow
      End If
    Next aLayer
  Next aPage
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #3  
Old 12-03-2018, 08:44 PM
ivano ivano is offline Windows 10 Office 2013
Novice
 
Join Date: Dec 2018
Posts: 3
ivano is on a distinguished road
Default

That worked perfectly... and much easier to add additional controls with your code version.
I added the checkboxes from Developer tab....Insert...Check Box (ActiveX Control).


For any other non-VBA coder who might happen to come across this post because of they have the same need as me:

added page 1 in front of my document;
assigned only one layer to the objects (must be only one layer assigned) which I wanted to hide/show throughout the document;
added the checkboxes, as just described above, into page 1;
pasted the macro into the macro enabled file;
ensured the checkbox number given by Visio and the assigned layer name corresponds to the appropriate macro section.


Now with a simple check or uncheck that layer becomes visible or hidden in all the pages throughout the entire document.


Thanks very much!
Reply With Quote
  #4  
Old 12-03-2018, 10:17 PM
Guessed's Avatar
Guessed Guessed is offline Windows 10 Office 2016
Expert
 
Join Date: Mar 2010
Location: Canberra/Melbourne Australia
Posts: 963
Guessed is on a distinguished road
Default

As another minor cleanup, I would make use of the checkbox captions to make it more obvious how each checkbox aligns to a layer.
Make sure the checkbox caption is the same as the layer name it reveals and change all the checkbox macros to reflect the text in the checkbox caption eg.
Code:
Public Sub CheckBox2_Click()
  ShowHideLayer CheckBox2.Caption, CheckBox2.Value
End Sub
If this was done, you could then go even cleaner and pass in just the control itself and get the two values from the control itself
Code:
Public Sub CheckBox1_Click()
  ShowHideLayer2 CheckBox1
End Sub

Public Sub CheckBox2_Click()
  ShowHideLayer2 CheckBox2
End Sub

Public Sub CheckBox3_Click()
  ShowHideLayer2 CheckBox3
End Sub

Public Sub ShowHideLayer2(aCheckbox As Object)
  Dim aPage As Visio.Page, aLayer As Visio.Layer
  For Each aPage In ActiveDocument.Pages
    For Each aLayer In aPage.Layers
      If aLayer.Name = aCheckbox.Caption Then
        aLayer.CellsC(visLayerVisible).Formula = aCheckbox.Value
      End If
    Next aLayer
  Next aPage
End Sub
__________________
Andrew Lockton
Chrysalis Design, Melbourne Australia
Reply With Quote
  #5  
Old 12-04-2018, 07:10 AM
ivano ivano is offline Windows 10 Office 2013
Novice
 
Join Date: Dec 2018
Posts: 3
ivano is on a distinguished road
Default

I tried out the tweaked version and worked great too.


thanks again
Reply With Quote
Reply

Thread Tools
Display Modes


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with show/hide content control acontreras Word VBA 0 10-21-2014 07:28 PM
Tab symbols display with Show/Hide off Lowenburg Word 3 02-01-2012 03:57 PM
Show Hide info trintukaz Word 3 12-29-2011 12:23 AM
VBA to show/hide tables ccordner Word VBA 2 07-04-2011 03:00 AM
VBA to show/hide tables ccordner Outlook 0 07-01-2011 05:10 AM


All times are GMT -7. The time now is 12:59 AM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2018, Jelsoft Enterprises Ltd.
SEO by vBSEO ©2011, Crawlability, Inc.
MSOfficeForums.com is not affiliated with Microsoft