Link drawing views to BOM in SolidWorks using a custom VBA macro built on the SolidWorks API—a must‑have SolidWorks automation for any design engineer. This lightweight API macro scans your drawing’s FeatureManager tree to identify the active BOM feature and, with a single command, sets Keep linked to BOM on every model view. No more manual property tweaking or risk of orphaned views slipping through revisions. This my Popular article about SolidWorks API VBA Macro- SolidWorks Automation
Table of Contents
ToggleUsage of SolidWorks VBA Macro-Link views to BOM
Open your drawing sheet that already contains a BOM table.
Run the VBA macro from Tools → Macro → Run.
Watch the automation link all views to your BOM instantly. Refer this Article to know Reasons to use SolidWorks VBA Macro
Benefits of Link Drawing Views to BOM in SolidWorks using VBA API macro
Time Savings: Eliminate repetitive clicks—automate view‑to‑BOM linking across single or multi‑sheet drawings.
Consistency & Accuracy: SolidWorks automations ensure every view stays synchronized with your BOM, reducing errors during updates or revisions.
Scalability: Whether you’re working on simple two‑view drawings or complex assemblies with dozens of views, this API macro handles it all in one pass.
Prerequisites for Link Drawing Views to BOM in SolidWorks using VBA API macro
Before you begin, make sure you have:
SolidWorks (2018 or later) installed with the VBA editor enabled.
A drawing document open that already contains a Bill of Materials (BOM) table.
Basic familiarity with the SolidWorks VBA API and how to run macros.
Prerequisites & VBA References Setup
Before you paste and run the macro, you need to configure SolidWorks and the VBA editor so the API calls resolve correctly.
Enable VBA & Trust Settings
In SolidWorks, go to Options → System Options → General and check “Enable VBA macros”.
In the Windows Trust Center (if prompted), allow “Trust access to the VBA project object model.”
Open the VBA Editor
In SolidWorks, navigate to Tools → Macro → New.
Save the macro file (e.g.
LinkViewsToBOM.swp
).The VBA IDE opens automatically.
Add SolidWorks API References
In the VBA IDE, select Tools → References… and check the following libraries (version numbers may vary):SolidWorks 20XX Type Library
SolidWorks 20XX Constant Type Library. Refer here about SolidWorks API library
(Optional, but helpful) SolidWorks 20XX CommandManager Type Library
These references expose the
SldWorks.*
objects,swDocDRAWING
constants, and methods likeGetFirstView
.Paste the Macro Code
In Module1, delete any default text.
Copy the complete macro (both
Sub main()
andFunction getBomName(...)
withOption Explicit
) and paste it into the module.
Save & Compile
Click Debug → Compile VBAProject.
Ensure there are no compile errors (missing references will show “User-defined type not defined”).
Run the Macro
Back in SolidWorks, go to Tools → Macro → Run.
Select your saved macro file and click Run.
Once set up, the macro will:
Detect your active drawing.
Locate the BOM feature via the SolidWorks API.
Loop through every view and set Keep linked to BOM.
Prompt you to rebuild (Ctrl + B) so changes take effect.
By following these setup steps, you’ll be ready to link drawing views to BOM in SolidWorks with a single click—boosting your SolidWorks automations and streamlining your drawing workflows.
What This Link Drawing Views to BOM in SolidWorks VBA Macro Will Do?
Validate: Ensure a drawing document is active.
Discover: Traverse the FeatureManager to locate the BOM (or weldment table) feature.
Extract: Grab the feature’s internal name (e.g., “BomFeat1”).
Link: Iterate through all drawing views and link them to the BOM automatically.
Notify: Prompt you to rebuild the drawing to finalize the links.
Step‑by‑Step Guide to Link Drawing Views to BOM in SolidWorks using VBA Macro?
1. Setting Up the VBA Environment
In SolidWorks, go to Tools → Macro → New.
Save your macro file (e.g.,
LinkViewsToBOM.swp
).The VBA editor opens. Paste code into the
Module1
code window.
2. Checking for an Open Drawing
You only want this macro to run when a drawing document is active. Use:
Line‑by‑Line Explanation
Enable strict variable declaration
Option Explicit
Forces you to declare every variable with
Dim
.Helps catch typos and ensures you know exactly what variables exist.
Start of the main routine
Sub Main()
Defines the entry point for the macro.
Everything the macro does will be nested inside this
Sub
.
Declare SolidWorks application and document objects
Dim swApp As SldWorks.SldWorks ' SolidWorks application interface
Dim swModel As SldWorks.ModelDoc2 ' Any open document (part, assembly, or drawing)
Dim swDraw As SldWorks.DrawingDoc ' Specifically a drawing document
Dim swView As SldWorks.View ' Individual views on the drawing sheet
Dim BomName As String ' Internal name of the BOM feature
swApp
gives us methods to talk to SolidWorks.swModel
is a generic doc—later we’ll confirm it’s a drawing.swDraw
is our drawing-specific object once we’ve checked the type.swView
lets us loop through each view on the sheet.BomName
will hold the exact feature name of the BOM table.
Connect to the running SolidWorks instance
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
Application.SldWorks
is the COM entry point.ActiveDoc
returns whichever document (if any) is currently open and active.
Ensure the user has a drawing open
If swModel Is Nothing Or swModel.GetType <> swDocDRAWING Then
swApp.SendMsgToUser2 "Please open a drawing document!", _
swMbWarning, swMbOk
Exit Sub
End If
Checks two things:
swModel Is Nothing
: No document open at all.GetType <> swDocDRAWING
: The open doc isn’t a drawing.
If either is true, pop up a warning and stop the macro with
Exit Sub
.
Cast the generic model to a drawing document
Set swDraw = swModel
Now swDraw
exposes drawing‑specific methods like GetFirstView
.
Retrieve the BOM feature name
BomName = getBomName(swModel)
If BomName = "" Then Exit Sub
Debug.Print "BOM feature found: " & BomName
- Calls our helper function
getBomName
(detailed below). - If it returns an empty string, we assume no BOM was found and abort.
Debug.Print
writes the BOM name to the Immediate Window (useful for debugging).
Loop through all drawing views
Set swView = swDraw.GetFirstView ' First view is the sheet border
Set swView = swView.GetNextView ' Skip the sheet format itself
Do While Not swView Is Nothing
swView.SetKeepLinkedToBOM True, BomName
Set swView = swView.GetNextView
Loop
GetFirstView
actually returns the sheet frame, so we immediately move to the next.In the loop,
SetKeepLinkedToBOM True, BomName
turns on “Keep linked to BOM” for that view.We iterate until there are no more views on the sheet.
Notify the user and end the macro
MsgBox "All views are now linked to BOM '" & BomName & "'." & _
" Please rebuild the drawing (Ctrl+B).", vbInformation
End Sub
Helper function to find and return the BOM’s internal name
Function getBomName(swModel As ModelDoc2) As String
Dim swFeat As Feature
Dim swBomFeat As BomFeature
Set swFeat = swModel.FirstFeature
Do While Not swFeat Is Nothing
Select Case swFeat.GetTypeName
Case "BomFeat", "WeldmentTableFeat"
' Grab the BOM-specific feature
Set swBomFeat = swFeat.GetSpecificFeature2
getBomName = swBomFeat.Name
Exit Function
End Select
Set swFeat = swFeat.GetNextFeature
Loop
' If we exit the loop, no BOM was found
MsgBox "No BOM or weldment table found in this drawing.", vbCritical
getBomName = ""
End Function
FirstFeature
gives the top node in the FeatureManager tree.- We loop through all features using
GetNextFeature
. - When we hit either a
"BomFeat"
or"WeldmentTableFeat"
, we callGetSpecificFeature2
to access the BOM interface and extract its.Name
. - If the loop finishes with no match, we alert the user and return an empty string.
Compete Code of Link Drawing Views to BOM in SolidWorks using VBA Macro
Option Explicit
'===========================================================
' Macro: Link All Drawing Views to BOM
' Description: Automates setting “Keep linked to BOM” on
' every view in the active drawing.
'===========================================================
Sub main()
Dim swApp As SldWorks.SldWorks ' SolidWorks application object
Dim swModel As SldWorks.ModelDoc2 ' Active document (could be part, assembly, or drawing)
Dim swDraw As SldWorks.DrawingDoc ' Drawing document object
Dim swView As SldWorks.View ' Individual drawing view
Dim BomName As String ' Internal name of the BOM feature
' Connect to the running SolidWorks instance
Set swApp = Application.SldWorks
Set swModel = swApp.ActiveDoc
' 1) Validate that a drawing document is active
If swModel Is Nothing Or swModel.GetType <> swDocDRAWING Then
swApp.SendMsgToUser2 "Please open a drawing document!", _
swMbWarning, swMbOk
Exit Sub ' Stop if not a drawing
End If
' Cast the generic ModelDoc2 to a DrawingDoc
Set swDraw = swModel
' 2) Find the BOM feature’s internal name
BomName = getBomName(swModel)
If BomName = "" Then Exit Sub ' Abort if none found
Debug.Print "BOM feature found: " & BomName ' Output for debugging
' 3) Loop through all views on the sheet
Set swView = swDraw.GetFirstView
Set swView = swView.GetNextView ' Skip the sheet format/frame
Do While Not swView Is Nothing
' Link this view to the BOM by name
swView.SetKeepLinkedToBOM True, BomName
Set swView = swView.GetNextView ' Move to the next view
Loop
' 4) Inform the user to rebuild so links update
MsgBox "All views are now linked to BOM '" & BomName & "'. " & _
"Please rebuild the drawing (Ctrl+B).", vbInformation
End Sub
'===========================================================
' Function: getBomName
' Purpose : Traverse the FeatureManager tree to locate the
' BOM or weldment table feature and return its
' internal name.
' Returns : String (e.g., "BomFeat1"), or "" if not found
'===========================================================
Function getBomName(swModel As ModelDoc2) As String
Dim swFeat As SldWorks.Feature ' Iterator for features
Dim swBomFeat As SldWorks.BomFeature ' BOM-specific interface
' Start at the top of the FeatureManager tree
Set swFeat = swModel.FirstFeature
' Loop through each feature until we find the BOM or weldment table
Do While Not swFeat Is Nothing
Debug.Print "Checking feature type: " & swFeat.GetTypeName
Select Case swFeat.GetTypeName
Case "BomFeat", "WeldmentTableFeat"
' Found the BOM feature
Set swBomFeat = swFeat.GetSpecificFeature2
getBomName = swBomFeat.Name ' Capture its internal name
Exit Function
End Select
' Move to the next feature in the tree
Set swFeat = swFeat.GetNextFeature
Loop
' If we reach here, no BOM feature was found
MsgBox "No BOM or weldment table found in this drawing.", vbCritical
getBomName = "" ' Return empty to indicate failure
End Function
How to use Link Drawing Views to BOM in SolidWorks VBA Macro
Follow these simple steps to link drawing views to BOM in SolidWorks using our VBA macro. This Rank Math–friendly guide ensures you get optimal SEO value while streamlining your SolidWorks automations.
1. Prepare Your Drawing Document
Open the SolidWorks drawing that already contains your BOM table.
Ensure the BOM is placed on the active sheet and displays correctly.
2. Enable the VBA Macro Environment
In SolidWorks, go to Tools → Macro → New.
Save the new macro (e.g.,
LinkViewsToBOM.swp
).In the VBA editor, open Tools → References and check:
SolidWorks XX Type Library
SolidWorks Constant Type Library
3. Install the Macro Code
Copy the complete VBA code (shown above) into your module.
Verify the code starts with
Option Explicit
and includes both theSub main()
andFunction getBomName(...)
.
4. Run the Macro
In SolidWorks, navigate to Tools → Macro → Run.
Select your saved macro (
LinkViewsToBOM.swp
).Click Run—the macro will:
Detect your active drawing.
Find the BOM feature via the SolidWorks API.
Loop through each view and set Keep linked to BOM.
5. Verify the Links
After completion, a message box appears:
“All views are now linked to BOM ‘BomFeat1’. Please rebuild the drawing (Ctrl+B).”
Inspect any view’s properties: the Keep linked to BOM checkbox should be checked and reference your BOM.
6. Rebuild Your Drawing
Press Ctrl + B or click Rebuild to update all view links.
Confirm your views update automatically whenever the BOM changes.
Read Also
Further Reference
Frequently Asked Questions: Link Drawing Views to BOM in SolidWorks
1. What is the “Link Drawing Views to BOM in SolidWorks” macro?
The Link Drawing Views to BOM in SolidWorks macro is a VBA automation that scans your drawing’s FeatureManager tree, identifies the BOM feature, and automatically sets Keep linked to BOM on every model view. It saves you from manually toggling view‑by‑view in the drawing.
2. How do I install the “Link Drawing Views to BOM in SolidWorks” macro?
Open SolidWorks and go to Tools → Macro → New.
Save the macro (e.g.
LinkViewsToBOM.swp
).In the VBA editor, enable the SolidWorks XX Type Library and Constant Type Library under Tools → References.
Paste the complete macro code into
Module1
, save, and compile.
3. Can I run the macro on multi‑sheet drawings?
Yes. The macro loops through all views on the active sheet. To process other sheets, switch to each sheet and rerun the macro. You can also modify the code to iterate over all sheets automatically.
4. What happens if my drawing has multiple BOM tables?
The default macro finds the first BOM or weldment table in the FeatureManager. To target a specific BOM, adapt the getBomName
function to match by feature name or description before linking views.
5. How do I verify the views are linked after running the macro?
After the macro completes, check any view’s Properties in the drawing. The Keep linked to BOM checkbox should be checked and reference the BOM feature name (e.g., “BomFeat1”). Rebuild the drawing (Ctrl + B) to apply updates.
6. Can I unlink views from the BOM using this macro?
Yes. Change the SetKeepLinkedToBOM
call from True
to False
:
swView.SetKeepLinkedToBOM False, BomName
This will remove the link on each view instead of setting it.
7. What SolidWorks versions support this API macro?
Any SolidWorks 2015 or newer that supports VBA and the BomFeature
interface will work. Always confirm you have the correct Type Library references in VBA.
8. How do I customize the macro for a specific BOM feature name?
Inside getBomName
, after locating the BomFeat
or WeldmentTableFeat
, you can add a filter:
If swFeat.Name = "MyCustomBOMName" Then
getBomName = swFeat.Name
Exit Function
End If
This ensures only your named BOM is selected.
9. What if the macro can’t find a BOM in my drawing?
You’ll get a message box:
“No BOM or weldment table found in this drawing.”
Ensure you’ve inserted a BOM table and rebuilt the drawing. Then rerun the macro.
10. How does the macro handle the sheet format view?
The first view returned by GetFirstView
is the sheet format. The macro skips it by calling GetNextView
before the loop, ensuring only model views are processed.
11. Can I include this macro in my SolidWorks add‑in?
Absolutely. You can translate the VBA logic into C# or VB.NET within a SolidWorks add‑in, using the same API calls (GetFirstView
, SetKeepLinkedToBOM
, etc.) for a more integrated automation.
12. Will this macro work with weldment BOM tables?
Yes. The getBomName
function checks both "BomFeat"
and "WeldmentTableFeat"
feature types, so it links views to either standard or weldment BOMs.
13. How much time will this SolidWorks automation save me?
Instead of manually editing each view’s properties—especially in large drawings with dozens of views—this macro completes the task in seconds, saving potentially minutes to hours per drawing.
14. Is the “Link Drawing Views to BOM in SolidWorks” macro safe for production use?
Yes. It only modifies the Keep linked to BOM flag on views. Always save or back up your drawing before macro runs, but this routine has no other side effects on dimensions or annotations.
15. Can I schedule this macro to run automatically?
While VBA alone can’t schedule tasks, you could combine this macro with a Windows Task Scheduler script or a SolidWorks API add‑in that triggers on document open events—creating a fully automatic SolidWorks automation for your team.