How to Automatically Insert a Part into a New Assembly Using SolidWorks VBA
In day-to-day CAD work, engineers often create assemblies and manually insert parts one by one. While this may seem like a small task, repeating it many times across projects becomes inefficient.
Using SolidWorks VBA, this process can be automated. In this tutorial, we will create a macro that:
connects to the active SolidWorks session
reads the currently open part
creates a new assembly using the default template
inserts the part into the assembly
zooms to fit the model
This is a simple starting point for anyone learning SolidWorks automation.
Prerequisites
Before running this macro, make sure:
SolidWorks is installed
VBA macro environment is available
a part document is currently open in SolidWorks
the default assembly template is properly set in SolidWorks options
Use Case
This macro is useful when:
learning SolidWorks API basics
reducing repetitive assembly setup steps
building larger automation workflows
preparing for more advanced macros such as batch insertion or automatic mating
-
Step 1: Connect to SolidWorks
The macro first connects to the currently running SolidWorks application using:
Set swApp = Application.SldWorks
This gives access to the SolidWorks API.
-
Step 2: Read the Active Document
The macro checks which file is currently open in SolidWorks.
Set swDoc = swApp.ActiveDoc
If no file is open, the macro stops and shows an error message.
-
Step 3: Get the Active Part Title
The title of the active document is stored in a variable. This title is later used while inserting the part into the new assembly.
-
Step 4: Read the Default Assembly Template
Instead of hardcoding an assembly template path, the macro reads the default template configured in SolidWorks settings. This makes it more flexible.
-
Step 5: Create a New Assembly
A new assembly document is created from the default template.
-
Step 6: Insert the Part
The macro inserts the currently active part into the new assembly using AddComponent5.
-
Step 7: Fit the View
Finally, the macro adjusts the screen view so the inserted component is visible properly.
-
Step 8: Full Code
Option Explicit
' ==========================================================
' Macro Name : Create Assembly and Insert Active Part
' Author : Ramu Gopal, The Tech Thinker
' Purpose : Creates a new assembly and inserts the
'Website: thetechthinker.com
' currently active part into that assembly
' ==========================================================
' SolidWorks application object
Dim swApp As SldWorks.SldWorks
' Generic SolidWorks document object
Dim swDoc As SldWorks.ModelDoc2
' Assembly document object
Dim swAssembly As SldWorks.AssemblyDoc
' Component object for inserted part
Dim swComponent As SldWorks.Component2
' Variable to store the active part name
Dim partName As String
Sub main()
' ------------------------------------------------------
' Step 1: Connect to the running SolidWorks application
' ------------------------------------------------------
Set swApp = Application.SldWorks
' ------------------------------------------------------
' Step 2: Get the currently active document
' ------------------------------------------------------
Set swDoc = swApp.ActiveDoc
' Check whether any document is open
If swDoc Is Nothing Then
MsgBox "No active SolidWorks document found. Please open a part file first."
Exit Sub
End If
' ------------------------------------------------------
' Step 3: Get the title of the active document
' ------------------------------------------------------
partName = swDoc.GetTitle
' Validate the part title
If Len(partName) = 0 Then
MsgBox "Failed to retrieve the active part title."
Exit Sub
End If
' ------------------------------------------------------
' Step 4: Get the default assembly template path
' ------------------------------------------------------
Dim defaultAssemblyTemplate As String
defaultAssemblyTemplate = swApp.GetUserPreferenceStringValue( _
swUserPreferenceStringValue_e.swDefaultTemplateAssembly)
' Validate the template path
If Len(defaultAssemblyTemplate) = 0 Then
MsgBox "Failed to retrieve the default assembly template path."
Exit Sub
End If
' ------------------------------------------------------
' Step 5: Create a new assembly document
' ------------------------------------------------------
Set swDoc = swApp.NewDocument(defaultAssemblyTemplate, 0, 0, 0)
' Check whether the new assembly was created successfully
If swDoc Is Nothing Then
MsgBox "Failed to create a new assembly document."
Exit Sub
End If
' Assign the new document as an assembly object
Set swAssembly = swDoc
' ------------------------------------------------------
' Step 6: Insert the active part into the new assembly
' ------------------------------------------------------
Set swComponent = swAssembly.AddComponent5( _
partName, _
swAddComponentConfigOptions_CurrentSelectedConfig, _
"", _
False, _
"", _
0, 0, 0)
' Check whether the component was inserted successfully
If swComponent Is Nothing Then
MsgBox "Failed to insert the part into the assembly."
Exit Sub
End If
' ------------------------------------------------------
' Step 7: Zoom to fit the inserted component
' ------------------------------------------------------
swDoc.ViewZoomtofit2
' ------------------------------------------------------
' Step 8: Success message
' ------------------------------------------------------
MsgBox "New assembly created and active part inserted successfully."
End Sub
-
Step 9: Conclusion
Expected Output
After running the macro:
- a new assembly is created
- the active part is inserted at the origin
- the view is automatically zoomed to fit
- a success message is displayed
Notes
A few important points:
- this macro assumes the active document is a part
- if a drawing or assembly is active, insertion may fail
- GetTitle returns the visible title, so in more advanced workflows it is better to use the full file path
- this is a beginner-level example and can be expanded further
Possible Improvements
This macro can be improved in future versions to:
- verify that the active document is specifically a part file
- use full file path instead of only title
- insert multiple parts
- add mates automatically
- position components with coordinates
- drive insertion using Excel input
Conclusion
This example shows how SolidWorks VBA can automate even simple assembly creation tasks. While the macro is small, it demonstrates important API concepts such as document handling, template usage, component insertion, and workflow automation.
For engineers starting with SolidWorks API, this is a good foundational example before moving to more advanced macros.
Follow for more SolidWorks automation tutorials