I’ve been spinning my wheels all afternoon trying to figure out how to insert a part into the active assembly. I think i need to use AddComponent5, but the macro keeps puking on this line:
For reference, here’s the rest of that bit of code. i got so tired of the trial and error that i recorded a macro inserting a part and copy/paste into mine. i’m sure i dont need all that stuff in there.
Dim tmpObj As ModelDoc2
Set tmpObj = swApp.OpenDoc6(fullPartPath, 1, 32, "", errors, longwarnings)
Set swAssy = swApp.ActivateDoc3(AssemblyTitle, True, 0, errors)
Dim swInsertedComponent As Component2
Set swInsertedComponent = Part.AddComponent5(fullPartPath, 0, "", False, "", 0, 0, 0)
swApp.CloseDoc fullPartPath
Dim TransformData() As Double
ReDim TransformData(0 To 15) As Double
TransformData(0) = 1
TransformData(1) = 0
TransformData(2) = 0
TransformData(3) = 0
TransformData(4) = 1
TransformData(5) = 0
TransformData(6) = 0
TransformData(7) = 0
TransformData(8) = 1
TransformData(9) = 0
TransformData(10) = 0
TransformData(11) = 0
TransformData(12) = 1
TransformData(13) = 0
TransformData(14) = 0
TransformData(15) = 0
Dim TransformDataVariant As Variant
TransformDataVariant = TransformData
Dim swMathUtil As Object
Set swMathUtil = swApp.GetMathUtility()
Dim swTransform As Object
Set swTransform = swMathUtil.CreateTransform((TransformDataVariant))
boolstatus = swInsertedComponent.SetTransformAndSolve2(swTransform)
There’s nothing wrong with any of the code you’ve posted here, so there’s nothing we can do to help diagnose what’s wrong. “Puking” also gives no indication of what error is ocurring.
So basically what you need to do is start from working code and work toward your non-working code until you figure out what’s causing the problem
Here is about the simplest implementation of AddComponent5 you can get. Start from here and add bits of your code step by step until you figure out what line causes the error.
Sub main()
Dim swapp As SldWorks.SldWorks
Dim swDoc As SldWorks.ModelDoc2
Dim sPath As String
Dim swComp As SldWorks.Component2
Set swapp = Application.SldWorks
Set swDoc = swapp.ActiveDoc
sPath = "[your path here]"
Set swComp = swDoc.AddComponent5(sPath, 0, "", False, "", 0, 0, 0)
Debug.Print swComp Is Nothing
End Sub
That error would indicate that your variable named “Part” does not point to an assembly document. Where in your code do you have “Set Part = [something]”?
I have my part going into my assembly now. now I’m stuck on how to fix it. i’ve read through everything i can think of on that solidworks api and tried all different kinds of things, even recording and trying to use that code again… can i get one more assist here? thanks!
Option Explicit
Dim swApp As Object
Dim swAssy As SldWorks.AssemblyDoc
Dim AssemblyTitle As String
Dim vComps As Variant
Dim folderPath As String
Dim userPartName As String
Dim userPartNameWextension As String
Dim fullPartPath As String
Dim Part As Object
Dim swPart As SldWorks.PartDoc
Dim longstatus As Long
Dim longwarnings As Long
Dim boolstatus As Boolean
Dim errors As Long
Sub main()
Set swApp = Application.SldWorks
'Verify that an assy is the active document
If swApp.ActiveDoc Is Nothing Or swApp.ActiveDoc.GetType <> swDocASSEMBLY Then
MsgBox "Please open an assembly before running this macro.", vbCritical
Exit Sub
End If
'Get the assy title
Set swAssy = swApp.ActiveDoc
AssemblyTitle = swAssy.GetTitle
'Get file path of first component to save the new part to
vComps = swAssy.GetComponents(False)
If IsEmpty(vComps) Then
MsgBox "The assembly does not contain any components yet.", vbCritical
Exit Sub
End If
folderPath = Left$(vComps(0).GetPathName, InStrRev(vComps(0).GetPathName, "\"))
'Enter new part name
userPartName = InputBox("Enter the NEW part name (without extension):")
userPartNameWextension = userPartName & ".SLDPRT"
fullPartPath = folderPath & userPartNameWextension
'New part document
Set Part = swApp.NewDocument("C:\ProgramData\SolidWorks\SOLIDWORKS 2024\templates\Part.prtdot", 0, 0, 0)
Set swPart = Part
swApp.ActivateDoc2 "Part1", False, longstatus
Set Part = swApp.ActiveDoc
' Save as new part
longstatus = Part.SaveAs3(fullPartPath, 0, 0)
' Insert new part into the assembly
swApp.OpenDoc6 fullPartPath, 1, 32, "", errors, longwarnings
Set swAssy = swApp.ActivateDoc3(AssemblyTitle, True, 0, errors)
Dim swInsertedComponent As Component2
Set swInsertedComponent = swAssy.AddComponent5(fullPartPath, 0, "", False, "", 0, 0, 0)
swApp.CloseDoc fullPartPath
' Fix the new part
Set Part = swApp.ActiveDoc
boolstatus = Part.Extension.SelectByID2(userPartName, "COMPONENT", 0, 0, 0, False, 0, Nothing, 0)
Part.FixComponent
End Sub
userPartName is not the right syntax to refer to the component in your SelectByID2. You need at least an instance number… Look at the code in the example for
Resolve All Lightweight Components and Fix a Component (VBA)
Your string needs to fully specify the component. Like this
forget it. this is such a pain in the butt. all i want to do is fix the part that was just inserted in the assy. but you cant do that in one line of code. it seems you have to jump through hoops to do any one thing in here.. i’ve been at this for three hours now and getting nowhere. now i remember why the last time i wrote a macro in sw was the last time.
I’m not an AI person by any means, but I thought i’ll plug it in and see what happens.. I might start doing that more if I need to fumble through the solidworks api ever again. i shouldve done that 10 hours ago!
Sub main()
Dim swApp As SldWorks.SldWorks
Dim swAssy As SldWorks.AssemblyDoc
Dim swPart As SldWorks.PartDoc
Dim swInsertedComp As SldWorks.Component2
Dim assyTitle As String
Dim comps As Variant
Dim folderPath As String
Dim partName As String
Dim partPath As String
Dim errors As Long
Dim status As Boolean
Set swApp = Application.SldWorks
' Make sure it's assy
If swApp.ActiveDoc Is Nothing _
Or swApp.ActiveDoc.GetType <> swDocASSEMBLY Then
MsgBox "Please open an assembly before running this macro.", vbCritical
Exit Sub
End If
Set swAssy = swApp.ActiveDoc
assyTitle = swAssy.GetTitle
' Get file path
comps = swAssy.GetComponents(False)
If IsEmpty(comps) Then
MsgBox "The assembly does not contain any components.", vbCritical
Exit Sub
End If
folderPath = Left$(comps(0).GetPathName, _
InStrRev(comps(0).GetPathName, "\"))
' Ask for part name
partName = Trim$(InputBox("Enter the NEW part name (without extension):"))
If partName = "" Then Exit Sub
partPath = folderPath & partName & ".SLDPRT"
' Create and save new part
Set swPart = swApp.NewDocument( _
"C:\ProgramData\SolidWorks\SOLIDWORKS 2024\templates\Part.prtdot", _
0, 0, 0)
swPart.SaveAs3 partPath, 0, 0
' Activate assembly
swApp.ActivateDoc3 assyTitle, True, 0, errors
Set swAssy = swApp.ActiveDoc
' Insert part into assembly
Set swInsertedComp = swAssy.AddComponent5( _
partPath, 0, "", False, "", _
0#, 0#, 0#)
' Close part
swApp.CloseDoc partName & ".SLDPRT"
' Fix inserted component
If Not swInsertedComp Is Nothing Then
swAssy.ClearSelection2 True
status = swInsertedComp.Select4(False, Nothing, False)
swAssy.FixComponent
End If
' Rebuild assy
swAssy.EditRebuild3
' Edit part mode
If Not swInsertedComp Is Nothing Then
swAssy.ClearSelection2 True
swInsertedComp.Select4 False, Nothing, False
swAssy.EditPart
End If
End Sub