I have a simple project which involves the use of several buttons in order to make various datasets visible depending on which category is required at the time.

It works fine using the popular GetLayerByName function (http://bit.ly/Ha2iGK) but unfortunately there are duplicate layer names scattered across the mxd. To get around this I modified it slightly to look at both the layer name and the layer description, but as raster catalogs are unable to store a description (this is unfortunately not in a geodatabase in case that makes any difference) the code falls over.

I've got a separate routine that clears all of the layers once the button is clicked and then it turns on the layers listed under another sub using [layername], [layerdescription]:

    Set pLayer = GetLayerByName("Wet (483)", "L6_Dry")
If Not pLayer Is Nothing Then
    pLayer.Visible = True
End If

Does anyone have any ideas or suggestions on how I can get it to skip/ignore the catalogs? I've never had much dealing with them inside VBA so any help at all would be greatly appreciated.

Public Function GetLayerByName(sLayerName As String, sLayerDesc As String) As ILayer
Dim pMxDocument As IMxDocument
Dim pCompositeLayer As ICompositeLayer
Dim pMap As IMap
Dim l As Long
Dim m As Long

Set pMxDocument = ThisDocument
Set pMap = pMxDocument.FocusMap

For l = 0 To pMap.LayerCount - 1
    Set GetLayerByName = LayerByName(pMap.Layer(l), sLayerName, sLayerDesc)
    If Not GetLayerByName Is Nothing Then Exit Function
    Next l
End Function

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

Dim l As Long

Dim pLayerGenProp As ILayerGeneralProperties

'#### This is where the error appears!####

Set pLayerGenProp = pLayer

Dim thestring As String
thestring = pLayerGenProp.LayerDescription

If UCase$(pLayer.Name) = UCase$(sName) And UCase$(thestring) = UCase$(sLayerDesc) Then
    Set LayerByName = pLayer
    Exit Function
End If

If Not TypeOf pLayer Is IGroupLayer Then Exit Function

Set pCompositeLayer = pLayer
For l = 0 To pCompositeLayer.Count - 1

    Set pReturnLayer = LayerByName(pCompositeLayer.Layer(l), sName, sLayerDesc)

    If Not pReturnLayer Is Nothing Then
        Set LayerByName = pReturnLayer
        Exit Function
    End If
    Next l

End Function

You should test whether the layer is a Raster Catalog layer by using the code

If TypeOf pLayer Is IRasterCatalogLayer Then Exit Function
| improve this answer | |
  • doh! thanks both, kenbuja's suggestion worked perfectly. – Rich Stokes Apr 3 '12 at 10:35
  • Glad to help...don't forget to mark this as the answer. – kenbuja Apr 3 '12 at 13:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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