Author Topic: Loopin through Layers and skip a layer for if condition  (Read 221 times)

0 Members and 1 Guest are viewing this topic.


  • Newbie
  • *
  • Posts: 7
Loopin through Layers and skip a layer for if condition
« on: May 09, 2022, 07:49:25 AM »
Hello I am novice to VBA so please excuse me if the question is too simple.

I am writing set of codes on a Visio file that has multiple pages and each pages has many layer but one common layer called "P&ID". One of the code i am writing is to loop through each page starting from page 2 and loop through each layers and if there are no visible layers then hide the page. But when looping through layers i dont want to check the condition for visibility on "P&ID" layer (because this layer will always be on). I wrote this code but did not work and i know i made this complicated.

Public Sub ShowHidepage()
  Dim flg As Boolean
  Dim pag As Integer
  Dim j As Integer
  Dim c As Integer
  Set flg=false
   pag = ActiveDocument.Pages.Count
   For j = 2 To pag
   c = 1
    Do While ActiveDocument.Pages(j).Layers(c) <> "P&ID"
   If ActiveDocument.Pages(j).Layers(c).CellsC(visLayerVisible).FormulaU = "1" Then
             flg = True
            Exit Do
          flg = False
               End If      
              c = c + 1
            If flg = True Then
 ActiveDocument.Pages(j).PageSheet.CellsSRC(visSectionObject, visRowPage, visPageUIVisibility).FormulaU = "1"
        ActiveDocument.Pages(j).PageSheet.CellsSRC(visSectionObject, visRowPage, visPageUIVisibility).FormulaU = "0"
End If
Next j
End Sub


  • Hero Member
  • *****
  • Posts: 2950
Re: Loopin through Layers and skip a layer for if condition
« Reply #1 on: May 09, 2022, 09:35:59 AM »
"did not work" is a very poor diagnosis.

Public Sub ShowHidePage()

    Dim pg As Page
    Dim lyr As Layer
    Dim b_visible As Boolean
    For Each pg In ActiveDocument.Pages
        If pg.Index <> 1 Then
            b_visible = False
            For Each lyr In pg.Layers
                If lyr.Name <> "P&ID" Then
                    If lyr.CellsC(visLayerVisible).ResultIU = 1 Then
                        b_visible = True
                        Exit For
                    End If
                End If
            Next lyr
            If b_visible Then
                pg.PageSheet.Cells("UIVisibility").Formula = 0
                pg.PageSheet.Cells("UIVisibility").Formula = 1
            End If
        End If
    Next pg

End Sub
« Last Edit: May 10, 2022, 12:43:38 AM by Yacine »