Layers Off but only specific ones not in a sequential range.

Started by Biomedmike, December 13, 2024, 09:00:30 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

Biomedmike

Is there a way to turn off just layers  1, 3, 5, 6 and 12 using a single VBA line?


I learned from this group how to turn off a range and that works great. But now I want to select certain layers that not not in a range. Why is everyithing so hard to find related to Visio VBA. I hate to keep asking such basic questions but I have searced the Web for about an hour.

SetLayersVisible ActivePage, 1, 133, False   
' Use to turn range of layers off. Range in this case is Layer 1-133.

SetLayersVisible ActivePage, 1, 133, True   
' Use to turn range of layers on. Range in this case is Layer 1-133.



wapperdude

QuoteWhy is everything so hard to find related to Visio VBA
. The Cinderella syndrome.  Plus, lot fewer users, than, say Excel.  Thus, it doesn't get as much attention from either side:  My or users.

Fret not (unless you're a guitar player), even the chatAIs have trouble.   I find the they're
 moderately useful for syntax solutions. 

Best learning...
basic VBA from Excel..=> grammar, syntax structure, etc. 
The macro recorder. 
The free SDK downloads. 
Other user solutions. 
Try simple things just for sake of code development. 

Search the Forum for VBA learning.
Visio 2019 Pro

Biomedmike

Do you know of a way to turn off just layers  1, 3, 5, 6 and 12 using a single VBA line? (Layers that are not sequential. )

Thank you,

BiomedMike

wapperdude

Requires code if doing it manually not acceptable.

Is it always those layers, or was that just a for instance example?

Each layer needs to be told separately.  Don't recall if macro recorder will catch you doing that.  I'm not at my computer right now.
Visio 2019 Pro

Thomas Winkel

#4
There is no such thing as "Visio VBA".
Regardless if Excel, Powerpoint or Visio, VBA is always the same.
You already know everything about the Visio object model to solve this task.
What is still missing is general VBA knowledge about data types, arrays, dictionaries, etc.
Start with the function declaration and a test function and then fill it with code.

Intuitively you would start with an integer array:
Option Explicit

Public Sub SetLayersVisible(vsoPage As Visio.Page, layerIndices() As Integer, isVisible As Boolean)
    ' Your code here
End Sub

Private Sub TestSetLayersVisible()
    Dim layerIndices(0 To 5) As Integer
   
    layerIndices(0) = 1
    layerIndices(1) = 3
    layerIndices(2) = 5
    layerIndices(3) = 7
    layerIndices(4) = 9
    layerIndices(5) = 11
   
    SetLayersVisible ActivePage, layerIndices, False
End Sub

But it is easier to use Variant because then you can use Array() for initializing and 'For Each' in your code:
Option Explicit

Public Sub SetLayersVisible(vsoPage As Visio.Page, layerIndices As Variant, isVisible As Boolean)
    ' Your code here
End Sub

Private Sub TestSetLayersVisible()
    SetLayersVisible ActivePage, Array(1, 3, 5, 7, 9, 11), False
End Sub

For reference, we come from this topic:
https://visguy.com/vgforum/index.php?topic=10580

Biomedmike

Quote from: wapperdude on December 14, 2024, 02:55:04 PMRequires code if doing it manually not acceptable.

Is it always those layers, or was that just a for instance example?


That was just an example. I have so many lines of code to turn off random layers for a specific step. I was hoping to change the many (many lines) to a single line to turn off the layers I need off at the moment.

BiomedMike

wapperdude

This older post has some basic concepts regarding showing/hiding layers.  These would be in addition to what Thomas has presented.

The biggest issue to determining how to select desired layers.  Somehow, need to efficiently step thru the available layers and select the ones desired.  Just a crude step thru seems very inefficient considering the number of available candidates...But it is relatively easy to implement.  If the layers could be grouped that could help.  But, just to go thru 100 layers or so will take some creative organizing.
Visio 2019 Pro

Thomas Winkel

Ok, I solve...

With integer array:
Option Explicit

Public Sub SetLayersVisible(vsoPage As Visio.Page, layerIndices() As Integer, isVisible As Boolean)
    Dim i As Integer
    Dim index As Integer
    Dim vsoLayer As Visio.layer
   
    For i = LBound(layerIndices) To UBound(layerIndices)
        index = layerIndices(i)
        Set vsoLayer = vsoPage.Layers(index)
        vsoLayer.CellsC(visLayerVisible) = isVisible
        vsoLayer.CellsC(visLayerPrint) = isVisible
    Next i
End Sub

Sub TestSetLayersVisible()
    Dim layerIndices(0 To 5) As Integer
    layerIndices(0) = 1
    layerIndices(1) = 3
    layerIndices(2) = 5
    layerIndices(3) = 7
    layerIndices(4) = 9
    layerIndices(5) = 11
   
    SetLayersVisible ActivePage, layerIndices, True
End Sub

With variant:
Option Explicit

Public Sub SetLayersVisible(vsoPage As Visio.Page, layerIndices As Variant, isVisible As Boolean)
    Dim index As Variant
    Dim vsoLayer As Visio.layer
   
    For Each index In layerIndices
        Set vsoLayer = vsoPage.Layers(index)
        vsoLayer.CellsC(visLayerVisible) = isVisible
        vsoLayer.CellsC(visLayerPrint) = isVisible
    Next index
End Sub

Private Sub TestSetLayersVisible()
    SetLayersVisible ActivePage, Array(1, 3, 5, 7, 9, 11), False
End Sub

wapperdude

@Thomas:  the main issue, as I understand, is the ability to select an arbitrary collection of layers which are not necessary in a contiguous sequence from over 100s of layers. 

There are a few thing not clear...
  > how many pages in document
  > how many layers on a page
  > do some pages have common layers
  > do shapes have more than 1 layer
  > can selected layers be identified via selected shapes.  That is, if shape A has layers 7, 12, 33: if those layers are set to hidden, is that sufficient?
  > does a common layer get set to hidden for all pages or just the active page?

Since vba syntax is set layer.cell(visible).formula = value, I don't see a single line solution as being possible.

Anyway, those are issues that impact solution and approach as I understand.
Visio 2019 Pro

Nikolay

If you are not a fan of VBA programming, there are Visio layer managers available, maybe you will find those helpful.

You could take a look at this one from David Parker (you can define "layer sets" there to turn them on/off at once):
https://bvisual.net/products/layermanager/



You can also take a look at mine, it is much less sophisticated (does not have "layer sets"), basically just adds "filter layer by name" box. I did not make that into a proper product, but still just using it myself for convenience from time to time.
https://github.com/nbelyh/LayerHelper
https://github.com/nbelyh/LayerHelper/releases/download/1.0.3648/LayerHelper-1.0.3648.msi



Browser ID: smf (possibly_robot)
Templates: 4: index (default), Display (default), GenericControls (default), GenericControls (default).
Sub templates: 6: init, html_above, body_above, main, body_below, html_below.
Language files: 4: index+Modifications.english (default), Post.english (default), Editor.english (default), Drafts.english (default).
Style sheets: 4: index.css, attachments.css, jquery.sceditor.css, responsive.css.
Hooks called: 332 (show)
Files included: 34 - 1306KB. (show)
Memory used: 1207KB.
Tokens: post-login.
Cache hits: 13: 0.00129s for 26,587 bytes (show)
Cache misses: 3: (show)
Queries used: 19.

[Show Queries]