3

Trying to get the layer names from a group that are inside a group. I have the code the gets the layer from a group, but not group inside group. any ideas?

Dim pDoc As IMxDocument
Set pDoc = ThisDocument
Dim pMap As IMap
Set pMap = pDoc.FocusMap

Dim pCompLayer As ICompositeLayer
Dim pCompLayer_child As ILayer
Dim pGrplyr As IGroupLayer
Dim i As Integer
Dim l As Integer
Dim k As Integer



For i = 0 To pMap.LayerCount - 1
 If TypeOf pMap.Layer(i) Is IGroupLayer Then
 Set pCompLayer = pMap.Layer(i)

For l = 0 To pCompLayer.Count - 1
 UserForm1.ComboBox1.AddItem pCompLayer.Layer(l).Name

 ' detect if group is within group?? stuck here
 Set pCompLayer_child = pCompLayer.Layer(l)
 If TypeOf pCompLayer_child Is IGroupLayer Then
 Set pCompLayer_child = pCompLayer.Layer(l)

  UserForm1.ComboBox1.AddItem pCompLayer_child.Name

 End If

Next l


End If
Next i

UserForm1.Show

End Sub

3 Answers 3

4

how about something like this? it'll get all layers in the TOC...

        Dim pMxDoc As IMxDocument = TryCast(m_application.Document, IMxDocument)
        Dim pMap As IMap = pMxDoc.FocusMap

        Dim pEnumLayer As IEnumLayer = pMap.Layers
        pEnumLayer.Reset()

        Dim pLayer As ILayer = pEnumLayer.Next
        Dim pFLayer As IFeatureLayer = Nothing
        Do While Not pLayer Is Nothing
            If TypeOf pLayer Is IFeatureLayer Then 
                UserForm1.ComboBox1.AddItem pLayer.Name
                Exit Do
            End If
            pLayer = pEnumLayer.Next
        Loop
1
  • Thanks for the reply. I found the code works but will not list the layers at the bottom of the list. I had to modify the code for VBA. I had to add the for loops so it will show all the layers. Without the for loop it just shows the first nested group and thats it. But not sure why it misses off the last 8 layers in the TOC. Please see code below
    – Daz
    Commented Nov 21, 2012 at 11:37
1
If TypeOf pMap.Layer(i) Is IGroupLayer Then

you could use recursion to go inside of the grouplayers until all groups are searched. A good example was found at esri forums

Just copying the recursive part here:

Private Function LayerByName(pLayer As ILayer, sName As String) As ILayer
     Dim pReturnLayer As ILayer
     Dim pCompositeLayer As ICompositeLayer
     Dim l As Long

     ' Set return layer and exit if this layer is the layer being sought.
     If UCase$(pLayer.Name) = UCase$(sName) Then
          Set LayerByName = pLayer
          Exit Function
     End If

     ' Exit if layer is not a group layer.  Not setting return value will cause search to continue.
     If Not TypeOf pLayer Is IGroupLayer Then Exit Function

     ' Process each layer in the group layer.
     Set pCompositeLayer = pLayer
     For l = 0 To pCompositeLayer.Count - 1
          ' Use recursion to traverse nested group layers.
          Set pReturnLayer = LayerByName(pCompositeLayer.Layer(l), sName)

          ' If layer was found, set return value and exit.
          If Not pReturnLayer Is Nothing Then
               Set LayerByName = pReturnLayer
               Exit Function
          End If
     Next l
End Function
0
 Dim pDoc As IMxDocument
      Set pDoc = ThisDocument
    'pMxDoc = TryCast(m_application.Document, IMxDocument)
    Dim pMap As IMap
   Set pMap = pDoc.FocusMap


    Dim pEnumLayer As IEnumLayer
   Set pEnumLayer = pMap.Layers
    pEnumLayer.Reset


    For i = 0 To pMap.LayerCount - 1
     If TypeOf pMap.Layer(i) Is IGroupLayer Then
     Set pCompLayer = pMap.Layer(i)
   For l = 0 To pCompLayer.Count - 1
    ' UserForm1.ComboBox1.AddItem pCompLayer.Layer(l).Name
    Dim pLayer As ILayer
    Set pLayer = pEnumLayer.Next

    Dim pFLayer As IFeatureLayer
    Set pFLayer = Nothing

    Do While Not pLayer Is Nothing
        If TypeOf pLayer Is IFeatureLayer Then

            UserForm1.ComboBox1.AddItem pLayer.Name

            Exit Do


        End If

        Set pLayer = pEnumLayer.Next

    Loop

    Next l

   End If
    Next i
1
  • Ok solved it by removing the if statement and changing the line For l = 0 To pCompLayer.Count - 1 To For l = 0 To 100 - 1
    – Daz
    Commented Nov 22, 2012 at 9:09

Not the answer you're looking for? Browse other questions tagged or ask your own question.