Get Components in Each BOM Table Row Example (VB.NET)

Win7 x64 Professional, SolidWorks 2011...

I am using a modified version of the sample code Get Components in Each BOM Table Row Example (VB.NET) that is in the help with SolidWorks.

Just wondering if anyone else has used it at all.

I get an access violation error on this line of code (even when using an unmodified version of the code):

swBomFeat = swFeat.GetSpecificFeature2

This only happens on some drawing and not others and appears to be random. Anyone have any ideas on how to fix this or do things different?

Here is the code:

Sub ProcessTableAnn(ByVal swApp As SldWorks, ByVal swModel As ModelDoc2, ByVal swTableAnn As TableAnnotation, ByVal ConfigName As String)

        Dim nNumRow As Long
        Dim J As Long
        Dim I As Long
        Dim ItemNumber As String = Nothing
        Dim PartNumber As String = Nothing

        Debug.Print("   Table Title        " & swTableAnn.Title)

        nNumRow = swTableAnn.RowCount

        Dim swBOMTableAnn As BomTableAnnotation
        swBOMTableAnn = swTableAnn

        For J = 0 To nNumRow - 1
            Debug.Print("   Row Number " & J & " Component Count  : " & swBOMTableAnn.GetComponentsCount2(J, ConfigName, ItemNumber, PartNumber))
            Debug.Print("       Item Number  : " & ItemNumber)
            Debug.Print("       Part Number  : " & PartNumber)

            Dim vPtArr As Object
            Dim swComp As Object
            Dim pt As Object

            vPtArr = swBOMTableAnn.GetComponents2(J, ConfigName)

            If (Not vPtArr Is Nothing) Then
                For I = 0 To UBound(vPtArr)
                    pt = vPtArr(I)
                    swComp = pt
                    If Not swComp Is Nothing Then
                        Debug.Print("           Component Name :" & swComp.Name2 & "      Configuration Name : " & swComp.ReferencedConfiguration)
                        Debug.Print("           Component Path :" & swComp.GetPathName)
                    Else
                        Debug.Print("  Could not get component.")
                    End If
                Next
            End If
        Next J
    End Sub


    Sub ProcessBomFeature(ByVal swApp As SldWorks, ByVal swModel As ModelDoc2, ByVal swBomFeat As BomFeature)

        Dim swFeat As Feature
        Dim vTableArr As Object
        Dim vTable As Object
        Dim vConfigArray As Object
        Dim vConfig As Object
        Dim ConfigName As String
        Dim swTable As TableAnnotation

        swFeat = swBomFeat.GetFeature
        vTableArr = swBomFeat.GetTableAnnotations

        For Each vTable In vTableArr
            swTable = vTable
            vConfigArray = swBomFeat.GetConfigurations(True, True)
            For Each vConfig In vConfigArray
                ConfigName = vConfig
                Debug.Print("-------------------------------------------------------")
                Debug.Print(" Component for Configuration : " & ConfigName)
                ProcessTableAnn(swApp, swModel, swTable, ConfigName)
            Next vConfig
        Next vTable

    End Sub


    Sub main()

        Dim swModel As ModelDoc2
        Dim swDraw As DrawingDoc
        Dim swFeat As Feature
        Dim swBomFeat As BomFeature

        swModel = swApp.ActiveDoc
        swDraw = swModel
        swFeat = swModel.FirstFeature

        Do While Not swFeat Is Nothing
            If "BomFeat" = swFeat.GetTypeName Then
                Debug.Print("******************************")
                Debug.Print("Feature Name : " & swFeat.Name)

                swBomFeat = swFeat.GetSpecificFeature2

                ProcessBomFeature(swApp, swModel, swBomFeat)
            End If
            swFeat = swFeat.GetNextFeature
        Loop
    End Sub


SolidworksApi macros