Hide Table of Contents

Insert BOM Table and Extract Data Example (VBA)

This example shows how to insert a BOM table and extract data from it.

'-------------------------------------------------------
' Preconditions:
' 1. Verify that the specified drawing document to open
'    exists.
' 2. Open the Immediate window.
'
' Postconditions:
' 1. Opens the specified drawing document.
' 2. Selects a drawing view.
' 3. Inserts a BOM table at the point where the drawing
'    was selected.
' 4. Examine the drawing and Immediate window.
'
' NOTE: Because the drawing is used elsewhere, do not
' save changes.
'-------------------------------------------------------
Option Explicit
Dim swApp As SldWorks.SldWorks
Dim swModel As SldWorks.ModelDoc2
Dim swModelDocExt As SldWorks.ModelDocExtension
Dim swDrawing As SldWorks.DrawingDoc
Dim swSelMgr As SldWorks.SelectionMgr
Dim swView As SldWorks.View
Dim swBomTable As SldWorks.BomTableAnnotation
Dim swTable As SldWorks.TableAnnotation
Dim vPickPt As Variant
Dim nNumCol As Long
Dim nNumRow As Long
Dim sRowStr As String
Dim i As Long
Dim j As Long
Dim bRet As Boolean
Dim fileName As String
Dim errors As Long
Dim warnings As Long
Sub main()
    Set swApp = Application.SldWorks    
    'Open drawing document and select drawing view
    fileName = "C:\Users\Public\Documents\SOLIDWORKS\SOLIDWORKS 2018\samples\tutorial\driveworksxpress\mobile gantry.slddrw"
    Set swModel = swApp.OpenDoc6(fileName, swDocumentTypes_e.swDocDRAWING, swOpenDocOptions_e.swOpenDocOptions_Silent, "", errors, warnings)
    Set swDrawing = swModel
    Set swModelDocExt = swModel.Extension
    bRet = swDrawing.ActivateView("Drawing View4")
    bRet = swModelDocExt.SelectByID2("Drawing View4", "DRAWINGVIEW", 0.130207615492954, 0.11628112033195, 0, False, 0, Nothing, 0)    
    'Insert BOM table
    Set swSelMgr = swModel.SelectionManager
    Set swView = swSelMgr.GetSelectedObject6(1, -1)
    vPickPt = swSelMgr.GetSelectionPoint2(1, -1)
    Set swBomTable = swView.InsertBomTable2(False, vPickPt(0), vPickPt(1), swBOMConfigurationAnchorType_e.swBOMConfigurationAnchor_TopLeft, swBomType_e.swBomType_Indented, "", "")
    Set swTable = swBomTable
    nNumCol = swTable.ColumnCount
    nNumRow = swTable.RowCount    
    'List BOM contents
    For i = 0 To nNumRow - 1
        sRowStr = ""
        For j = 0 To nNumCol - 1
            sRowStr = sRowStr & swTable.Text2(i, j, True) & ","
        Next j
        Debug.Print Left(sRowStr, Len(sRowStr) - 1)
    Next i    
End Sub


Provide feedback on this topic

SOLIDWORKS welcomes your feedback concerning the presentation, accuracy, and thoroughness of the documentation. Use the form below to send your comments and suggestions about this topic directly to our documentation team. The documentation team cannot answer technical support questions. Click here for information about technical support.

* Required

 
*Email:  
Subject:   Feedback on Help Topics
Page:   Insert BOM Table and Extract Data Example (VBA)
*Comment:  
*   I acknowledge I have read and I hereby accept the privacy policy under which my Personal Data will be used by Dassault Systèmes

Print Topic

Select the scope of content to print:

x

We have detected you are using a browser version older than Internet Explorer 7. For optimized display, we suggest upgrading your browser to Internet Explorer 7 or newer.

 Never show this message again
x

Web Help Content Version: API Help (English only) 2018 SP05

To disable Web help from within SOLIDWORKS and use local help instead, click Help > Use SOLIDWORKS Web Help.

To report problems encountered with the Web help interface and search, contact your local support representative. To provide feedback on individual help topics, use the “Feedback on this topic” link on the individual topic page.