Exporting table to Excel with unchecked fields

2995
3
Jump to solution
07-16-2014 02:37 AM
ArminMueller
New Contributor III

Hi,

i have a problem when exporting a table from a featurelayer with TableToExcel if the featurelayer has unchecked fields. It comes the error message RuntimeError: Cannot find field. The same works fine if no unchecked fields are available.

Here is my code snippet:

Dim objFLayer as IFeatureLayer = me.Layer

Dim objGP As IGeoProcessor = New GeoProcessor

Dim strOutFile As String = IO.Path.Combine(ExportFactory.GetProjectPath(ExportPathEnum.ExportPathEmbfiles),

                                           Me.ID & ".xls")

Try

    objGP.OverwriteOutput = True

    objParameters = New VarArray

    ' Populate the variant array with parameter values.

    objParameters.Add(objFLayer)

    objParameters.Add(strOutFile)

    ' Execute the model tool by name.

    Dim objResult As IGeoProcessorResult = objGP.Execute("TableToExcel_conversion", objParameters, Nothing)

Catch ex As Exception

Finally

    System.Runtime.InteropServices.Marshal.ReleaseComObject(objGP)

    objGP = Nothing

End Try

Someone has an idea? Thank you

Armin

1 Solution

Accepted Solutions
ArminMueller
New Contributor III

Hi Duncan,

thank you for your solution. In the meantime i has also find a solution.

Insead of using the ilayer object as parameter input i use the ilayer.name now. After this change it works correct and only the checked fields will be used.

Armin

View solution in original post

3 Replies
DuncanHornby
MVP Notable Contributor

Armin,

I have been having a go in VBA at resolving your problem. I too can make it come up with an error message if 1 or more fields has been turned off. Unfortunately I have been unable to resolve it but I did come up with a partial fix that stopped it returning an error. So if you had a dataset with some fields turned off the following code allowed it to run but it ignored the turned off fields. The result is a table in Excel as if you had not turned off any fields...

I think this is a bug (hopefully the ESRI developers will pick up on this thread?) or requires some esoteric fix that is not documented anywhere!

So here is my partial fix:


Public Sub test()


    'Get Map


    Dim pMXDoc As IMxDocument


    Set pMXDoc = ThisDocument


    Dim pMap As IMap


    Set pMap = pMXDoc.FocusMap


    


    ' Get FeatureLayer (has some fields turned off)


    Dim pLayer As ILayer


    Set pLayer = pMap.Layer(0)


    Dim pFL As IGeoFeatureLayer


    Set pFL = pLayer


    


    ' Create geoProcessor


    Dim pGP As IGeoProcessor2


    Set pGP = New GeoProcessor


    pGP.OverwriteOutput = True


    pGP.AddOutputsToMap = False


        


    ' Create parameters


    Dim pVA As IVariantArray


    Set pVA = New VarArray


    With pVA


        .Add pFL.DisplayFeatureClass


        .Add "temp"


    End With




    'Make a tableView called "temp"


    Dim pRes As IGeoProcessorResult2


    Set pRes = pGP.Execute("MakeTableView_management", pVA, Nothing)


    If pRes.Status = esriJobFailed Then


        Debug.Print "Failed tableview"


        Exit Sub


    End If


    


    ' Create parameters


    With pVA


        .RemoveAll


        .Add "temp"


        .Add "c:\scratch\test.xls"


    End With


    


    ' Execute tool


    Set pRes = pGP.Execute("TableToExcel_conversion", pVA, Nothing)


    If pRes.Status = esriJobSucceeded Then


        Debug.Print "OK!"


    End If


End Sub




0 Kudos
ArminMueller
New Contributor III

Hi Duncan,

thank you for your solution. In the meantime i has also find a solution.

Insead of using the ilayer object as parameter input i use the ilayer.name now. After this change it works correct and only the checked fields will be used.

Armin

DuncanHornby
MVP Notable Contributor

Unbelievable! I spent nearly an hour looking at this problem as I would have typically passed in the IFeaturelayer object as a parameter. So all you had to do is pass in the layer name instead...

I wish ESRI would document these important but subtle differences.

0 Kudos