"Runtime Error 91: Object Variable or With Block Variable Not Set"

Subject: "Runtime Error 91: Object Variable or With Block Variable Not Set"

Hello all,

I'm currently working with VBA in SOLIDWORKS and encountering an error when trying to add components to an assembly. My code aims to create a new assembly and then add multiple box parts to it. However, I'm consistently receiving the error message: "Runtime Error 91: Object Variable or With Block Variable Not Set" when attempting to add the components.

Here's the relevant part of my code:

```vba
' Define the parameters of the box and the slope
basePoint = 0
length = 1
slope = 2

' Create a new assembly
Set Assembly = swApp.NewDocument("C:\\ProgramData\\SOLIDWORKS\\SOLIDWORKS 2022\\templates\\Assembly.asmdot", 0, 0, 0)

' Check if Assembly was created successfully
If Assembly Is Nothing Then
MsgBox "Failed to create assembly."
Exit Sub
Else
MsgBox "Assembly created successfully."
End If

For i = 1 To 10

' Insert the box part
Dim boxPartPath As String
boxPartPath = "C:\\Users\\lxb10110\\Documents\\Parts\\box" & i & ".SLDPRT"

' Check if the part file exists
If Not fso.FileExists(boxPartPath) Then
MsgBox "Part file does not exist: " & boxPartPath
Exit Sub
End If

' Add the part to the assembly
boolstatus = Assembly.AddComponent5(boxPartPath, 0, "", False, "", basePoint, basePoint * slope, 0)
If Err.Number <> 0 Then
MsgBox "Error adding component: " & Err.Description
Exit Sub
End If

' Update the basePoint for the next box
basePoint = basePoint + length
Next i
```

The error seems to occur when calling Assembly.AddComponent5. The Assembly object appears to be created successfully, and the box part files exist at the given paths. However, when AddComponent5 is called, it throws the error, suggesting the Assembly object is not set.

I've verified that the part files exist and that no other code or processes seem to be interfering with the Assembly object. I've also tried running the code on a different machine and reinstalling SOLIDWORKS, but the issue persists.

Any insights or advice on how to troubleshoot and resolve this issue would be greatly appreciated. Thanks in advance for your help!

Best Regards,