Read table content into array using SOLIDWORKS API

Edit ArticleEdit Article

This example demonstrates how to read the content of the selected table (Bill Of Materials, General Table, Cut-List Table etc.) into the 2-dimensional array using SOLIDWORKS API.

ITableAnnotation SOLIDWORKS API interface provides an access to the data of all table types.

Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swSelMgr As SldWorks.SelectionMgr
Dim swTableAnnotation As SldWorks.TableAnnotation

Sub main()

    Set swApp = Application.SldWorks
    Set swModel = swApp.ActiveDoc
    If Not swModel Is Nothing Then

        Set swSelMgr = swModel.SelectionManager
        Dim tableData() As String
        Set swTableAnnotation = swSelMgr.GetSelectedObject6(1, -1)
        If Not swTableAnnotation Is Nothing Then
            ReDim tableData(swTableAnnotation.RowCount - 1, swTableAnnotation.ColumnCount - 1)
            Dim i As Integer
            Dim j As Integer
            For i = 0 To swTableAnnotation.RowCount - 1
                For j = 0 To swTableAnnotation.ColumnCount - 1
                    tableData(i, j) = swTableAnnotation.Text(i, j)
            MsgBox "Please select table"
        End If
        MsgBox "Please open model"
    End If
End Sub


All articles and code at CodeStack are now open-source and hosted on GitHub. If you want to contribute by modifying existing articles and code snippets, submitting new ones, reporting errors and bugs etc. please follow this blog post for more information. We appreciate any contribution.

Product of Xarial Product of Xarial