Insert BOM Table and Get Data Example (VBA)
This example shows how to insert a BOM table and the extract the data
from it.
'--------------------------------------
'
' Preconditions:
' (1)
Drawing is open.
' (2)
View in open drawing is selected.
'
' Postconditions: BOM table is inserted at the point where
the view was selected.
'
'--------------------------------------
Option Explicit
Public Enum swBOMConfigurationAnchorType_e
swBOMConfigurationAnchor_TopLeft
= 1
swBOMConfigurationAnchor_TopRight
= 2
swBOMConfigurationAnchor_BottomLeft
= 3
swBOMConfigurationAnchor_BottomRight
= 4
End Enum
Public Enum swBomType_e
swBomType_PartsOnly
= 1
swBomType_TopLevelOnly
= 2
swBomType_Indented
= 3
End Enum
Sub main()
Dim
swApp As
SldWorks.SldWorks
Dim
swModel As
SldWorks.ModelDoc2
Dim
swSelMgr As
SldWorks.SelectionMgr
Dim
swView As
SldWorks.View
Dim
swBomTable As
SldWorks.BomTableAnnotation
Dim
swTable As
SldWorks.TableAnnotation
Dim
swAnn As
SldWorks.Annotation
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
Set
swApp = Application.SldWorks
Set
swModel = swApp.ActiveDoc
Set
swSelMgr = swModel.SelectionManager
Set
swView = swSelMgr.GetSelectedObject5(1)
vPickPt
= swSelMgr.GetSelectionPoint(1)
Set
swBomTable = swView.InsertBomTable2(
_
False,
_
vPickPt(0),
vPickPt(1), _
swBOMConfigurationAnchor_TopLeft,
_
swBomType_Indented,
_
"",
_
""):
Debug.Assert Not swBomTable Is Nothing
Set
swTable = swBomTable
Set
swAnn = swTable.GetAnnotation
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.text(i,
j) & ","
Next
j
Debug.Print
Left(sRowStr, Len(sRowStr) - 1)
Next
i
End Sub
'--------------------------------------