AnsweredAssumed Answered

Set target layer: ErrHRESULT E_FAIL has been returned from a call to a COM component.

Question asked by geonetadmin on Jan 5, 2011
Latest reply on Jan 5, 2011 by helenchu
Original User: helenchu

All,
I have Error HRESULT E_FAIL has been returned from a call to a COM component when trying to set a edit target layer.  Below is what I have (vs2008, addin button).  I could get the msgbox to show the FeatureLayerName fine then it throws error at the 'exit for'.  I tried to create a new clean mxd application, didn't fix it.  I looked everywhere but couldn't find a solution.  These line of codes worked in my vb 6.0 with a few changes.  In vb 6.0 I passed pApp as AppRef not as IApplication & pMap = pEditor.Map
Any help is greatly appreciated. 


Public Function setTargetLayer(ByVal strLayerName As String, ByVal pApp As ESRI.ArcGIS.Framework.IApplication) As IFeatureLayer

        Dim i, z As Integer
        Dim pMap As IMap
        Dim pComLayer As ICompositeLayer
        Dim pLayer As ILayer
        Dim pFlayer As IFeatureLayer
        Dim pEditLayers As IEditLayers
        Dim pUID As New UID
        Dim pEditor As IEditor
        Dim pDoc As IMxDocument



        pUID.Value = "esriEditor.Editor"
        pEditor = pApp.FindExtensionByCLSID(pUID)

        pDoc = pApp.Document
        pMap = pDoc.FocusMap
        pEditLayers = pEditor

        Try
            'Set Target Layer
            For i = 0 To pMap.LayerCount - 1

                If TypeOf pMap.Layer(i) Is IFeatureLayer Then

                    pLayer = pMap.Layer(i)
                    If UCase(pLayer.Name) = UCase(strLayerName) Then
                        If pEditLayers.IsEditable(pLayer) Then
                            pFlayer = pLayer
                            setTargetLayer = pFlayer
                            MsgBox(FeatureLayerName:" & pFlayer.Name)
                            Exit For

                        Else
                            MsgBox("this layer is not editable")
                            Exit Function
                        End If
                    End If
                Else
                    If TypeOf pMap.Layer(i) Is ICompositeLayer Then
                        pLayer = pMap.Layer(i)
                        pComLayer = pLayer
                        For z = 0 To pComLayer.Count - 1
                            If UCase(pComLayer.Layer(z).Name) = UCase(strLayerName) Then
                                If pEditLayers.IsEditable(pComLayer.Layer(z)) Then
                                    pFlayer = pComLayer.Layer(z)
                                    setTargetLayer = pFlayer
                                    Exit For
                                Else
                                    MsgBox("This layer is not editable")
                                    Exit Function
                                End If
                            End If
                        Next z
                    End If
                End If


            Next i


        Catch ex As Exception
            System.Windows.Forms.MessageBox.Show(ex.Message)
        End Try


    End Function

Attachments

Outcomes