How to Check BOM Availability in a SolidWorks Drawing Using VBA
In production design workflows, verifying SolidWorks Drawing BOM Availability is one of the most important quality checks before releasing drawings for manufacturing. A drawing may look complete, with views, dimensions, and annotations, but still be missing a Bill of Materials (BOM). When that happens, downstream teams such as procurement, assembly planning, or manufacturing may receive incomplete documentation.
This tutorial explains how to check SolidWorks Drawing BOM Availability using SolidWorks VBA through the SOLIDWORKS API. The macro analyzes the active drawing and determines whether a BOM exists by examining both table annotations and BOM features.
By implementing this simple automation check, engineering teams can integrate CAD validation into their workflow and reduce release mistakes.
-
Step 1: Understand Why BOM Availability Matters
In many engineering organizations, drawings pass through multiple stages before release. Designers generate the geometry, drafters finalize the drawing, and review teams verify compliance.
During these transitions, BOM tables may be accidentally removed or never inserted.
Common risks include:
- assemblies released without BOM tables
- incorrect part quantities in manufacturing instructions
- missing information in ERP or PLM systems
- manual rework during design review
Automating SolidWorks Drawing BOM Availability checks helps eliminate these issues early.
Key benefits of automation
- faster drawing validation
- reduced human error
- standardized quality checks
- easier integration with CAD automation workflows
This is where VBA automation inside SolidWorks becomes very useful.
-
Step 2: Understand BOM Structures in SolidWorks Drawings
In SOLIDWORKS, a Bill of Materials (BOM) represents structured information extracted from an assembly.
A BOM may vary from one organization to another depending on internal standards, but it typically includes:
- Item Number
- Part Number
- Description
- Quantity
- Material
When a BOM is inserted into a drawing, it appears as a table annotation. Internally, SolidWorks also stores BOM information as a feature tree element, commonly referred to as BomFeat.
So, detecting SolidWorks Drawing BOM Availability requires checking both:
- Table annotations for visible BOM tables
- Feature tree elements for BOM features
The macro below uses both detection mechanisms.
-
Step 3: Know the Common Reasons BOM Tables Go Missing
During everyday CAD work, BOM tables can disappear for several reasons.
Typical causes
- template mismatch
- sheet duplication issues
- assembly configuration changes
- manual deletion
- view restructuring
By verifying SolidWorks Drawing BOM Availability, engineers can make sure every released drawing contains the required BOM structure.
-
Step 4: Understand the API Objects Used
This macro uses several objects from the SOLIDWORKS API.
Key API objects
- SldWorks – main application interface
- ModelDoc2 – active document representation
- ModelDocExtension – access to advanced document functions
- Feature – feature tree traversal
BOM detection methods
The macro checks for BOMs in two stages:
Method A – Table Annotation Search
The macro queries the drawing for BOM table annotations.
Method B – Feature Tree Scan
The macro scans the feature tree looking for BomFeat.
This dual approach improves reliability.
-
Step 5: Insert the VBA Macro to check BOM
Below is the complete VBA macro used to check BOM availability in the active drawing.
'---------------------------------------------------------------------------
' Macro Name : Check SolidWorks Drawing BOM Availability
' Author : Ramu Gopal
' Website : The Tech Thinker
'
' Description:
' This macro checks whether a Bill Of Materials (BOM) exists in the active
' SolidWorks drawing document (.SLDDRW).
'
' The macro performs BOM detection using TWO methods:
'
' Method 1 (Preferred):
' Checks for BOM Table Annotations placed on drawing sheets.
'
' Method 2 (Fallback):
' Scans the Feature Tree for BOM features (BomFeat).
'
' This dual approach improves reliability because:
' - Some SolidWorks versions expose different API methods
' - A BOM feature may exist even if the table is hidden or removed
'
' Result:
' A message box displays whether a BOM exists and how many were detected.
'
' NOTE:
' This macro performs validation only. It does NOT modify the drawing.
'
'---------------------------------------------------------------------------
Option Explicit
'---------------------------------------------------------------------------
' Entry Point of the Macro
'---------------------------------------------------------------------------
Sub main()
' Reference to the SolidWorks application
Dim swApp As SldWorks.SldWorks
' Reference to the currently active document
Dim swModel As SldWorks.ModelDoc2
'-----------------------------------------------------------------------
' Try to obtain the running SolidWorks application instance
'-----------------------------------------------------------------------
On Error Resume Next
Set swApp = Application.SldWorks
On Error GoTo 0
' If SolidWorks cannot be accessed, stop execution
If swApp Is Nothing Then
MsgBox "Could not get SolidWorks application.", vbCritical
Exit Sub
End If
'-----------------------------------------------------------------------
' Get the active document
'-----------------------------------------------------------------------
Set swModel = swApp.ActiveDoc
' Ensure a document is currently open
If swModel Is Nothing Then
MsgBox "No active document.", vbExclamation
Exit Sub
End If
'-----------------------------------------------------------------------
' Validate that the active document is a DRAWING
'-----------------------------------------------------------------------
' BOM tables exist only inside drawing documents (.SLDDRW)
If swModel.GetType <> swDocDRAWING Then
MsgBox "Active doc is not a Drawing (.SLDDRW).", vbExclamation
Exit Sub
End If
'-----------------------------------------------------------------------
' Count the number of BOMs detected in the drawing
'-----------------------------------------------------------------------
Dim bomCount As Long
bomCount = CountBOMs(swModel)
'-----------------------------------------------------------------------
' Display result to the user
'-----------------------------------------------------------------------
If bomCount > 0 Then
MsgBox "BOM found. Count: " & bomCount, vbInformation
Else
MsgBox "No BOM found in this drawing.", vbInformation
End If
End Sub
'---------------------------------------------------------------------------
' Function : CountBOMs
'
' Purpose:
' Counts BOM tables present in the drawing using two detection strategies.
'
' Method A (Preferred):
' Detect BOM table annotations placed on drawing sheets.
'
' Method B (Fallback):
' Scan the FeatureManager tree for BOM features (BomFeat).
'
' Returns:
' Number of BOM objects detected
'---------------------------------------------------------------------------
Private Function CountBOMs(swModel As SldWorks.ModelDoc2) As Long
Dim total As Long
total = 0
'-----------------------------------------------------------------------
' METHOD A : Detect BOM Table Annotations
'
' Table annotations represent tables inserted in drawings such as:
' - BOM Tables
' - Revision Tables
' - Weldment Cut Lists
'
' Here we specifically search for Bill Of Materials tables.
'-----------------------------------------------------------------------
On Error Resume Next
Dim swExt As SldWorks.ModelDocExtension
' ModelDocExtension provides advanced document functions
Set swExt = swModel.Extension
Dim vTables As Variant
'-----------------------------------------------------------------------
' Some SolidWorks versions support GetTableAnnotations2
' Others support GetTableAnnotations
'
' We attempt both to maintain compatibility across versions.
'-----------------------------------------------------------------------
Err.Clear
vTables = Empty
' Try the newer API method first
vTables = CallByName(swExt, "GetTableAnnotations2", VbMethod, swTableAnnotation_BillOfMaterials)
If Err.Number = 0 Then
' Count returned BOM table annotations
total = total + SafeUBoundPlusOne(vTables)
Else
' If the newer API is unavailable, try the older method
Err.Clear
vTables = Empty
vTables = CallByName(swExt, "GetTableAnnotations", VbMethod, swTableAnnotation_BillOfMaterials)
If Err.Number = 0 Then
total = total + SafeUBoundPlusOne(vTables)
End If
End If
On Error GoTo 0
'-----------------------------------------------------------------------
' METHOD B : Feature Tree Scan (Fallback Detection)
'
' Even if the BOM table is hidden or removed, the BOM feature may
' still exist inside the FeatureManager tree.
'
' Therefore we scan the entire feature tree for "BomFeat".
'-----------------------------------------------------------------------
Dim swFeat As SldWorks.Feature
' Get the first feature in the FeatureManager tree
Set swFeat = swModel.FirstFeature
' Traverse the entire feature tree
Do While Not swFeat Is Nothing
' Compare feature type name with "BomFeat"
If StrComp(swFeat.GetTypeName2, "BomFeat", vbTextCompare) = 0 Then
total = total + 1
End If
' Move to the next feature
Set swFeat = swFeat.GetNextFeature
Loop
' Return the total number of BOM detections
CountBOMs = total
End Function
'---------------------------------------------------------------------------
' Helper Function : SafeUBoundPlusOne
'
' Purpose:
' Safely determine the number of elements in a Variant array.
'
' Why needed:
' API calls may return:
' - Empty
' - Non-array values
' - Variant arrays
'
' This helper prevents runtime errors when counting returned objects.
'
' Returns:
' Number of elements in the array
'---------------------------------------------------------------------------
Private Function SafeUBoundPlusOne(v As Variant) As Long
On Error Resume Next
If IsEmpty(v) Then
SafeUBoundPlusOne = 0
ElseIf IsArray(v) Then
SafeUBoundPlusOne = UBound(v) - LBound(v) + 1
Else
SafeUBoundPlusOne = 0
End If
On Error GoTo 0
End Function
-
Step 6: Understand the Macro Logic
This macro follows a structured validation workflow. Instead of relying on a single detection method, it uses two complementary strategies to improve reliability across different SolidWorks versions.
-
Step 7: Conclusion
Ensuring SolidWorks Drawing BOM Availability is a critical quality check in engineering documentation. A drawing without a BOM can create confusion in manufacturing, delay procurement, and increase the risk of release errors.
This tutorial demonstrated how a SolidWorks VBA macro can detect BOM presence using two reliable methods:
- table annotation detection
- feature tree scanning
This dual-method approach improves robustness and supports compatibility across different SolidWorks versions and drawing conditions.
In larger engineering environments where many drawings are reviewed daily, checks like this can reduce manual effort and improve documentation quality. As your CAD automation toolkit grows, this macro can also be expanded into more advanced validation workflows such as balloon checks, BOM template verification, or full drawing QA automation.
Shared by Ramu Gopal, Mechanical Design Engineer and CAD Automation Specialist, founder of The Tech Thinker. Follow for more SolidWorks automation tutorials