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

1250
2
01-05-2011 10:54 AM
by Anonymous User
Not applicable
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
0 Kudos
2 Replies
helenchu
Occasional Contributor II
I was wrong.  After taking a closer look the error was thrown at
pEditLayers.SetCurrentLayer(pFlayer, 1) in my form

codes:
pFlayer = setTargetLayer(strTargetLayer, m_application)  --> this function works fine
pEditLayers.SetCurrentLayer(pFlayer, 1)

any idea why I can't use pEditLayers.SetCurrentLayer?

Thank you
0 Kudos
helenchu
Occasional Contributor II
I figure it out.  I used the wrong subtype, it should be 0 instead of 1.
0 Kudos