Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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
share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

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
share|improve this answer
    
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 Nov 21 '12 at 11:37
add comment
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
share|improve this answer
add comment
 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
share|improve this answer
    
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 Nov 22 '12 at 9:09
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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