0

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
PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Apr 2, 2012 at 10:45
1

1 Answer 1

2

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

If TypeOf pLayer Is IRasterCatalogLayer Then Exit Function
answered Apr 2, 2012 at 15:09
2
  • doh! thanks both, kenbuja's suggestion worked perfectly. Commented Apr 3, 2012 at 10:35
  • Glad to help...don't forget to mark this as the answer. Commented Apr 3, 2012 at 13:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.