How to Fix 'Runtime Error 91' in Component-Insertion Macro?

Hello Community,

I am tweaking a recorded macro that opens an assembly file and adds a component into it (for this post, the macro's capabilities are simplified/censored), setting it at the assembly's origin. The macro gets stuck at "boolstatus = swInsertedComponent.SetTransformAndSolve2(swTransform)", after the transform matrix for the inserted component is set & solved ("Set swTransform = swMathUtil.CreateTransform((TransformDataVariant))").

This error states that "Object variable or With block variable was not set". I read that a macro-recorder does not capture all code, and that some script may be hard-coded and in need of rewriting to make the macro work. However, I cannot find instances of either in the script. I am also using this guide to hopefully find a fix: Object variable or With block variable not set - Visual Basic | Microsoft Docs 

The code is essentially untouched since it was recorded. What specific variable is Solidworks telling me to set, here?

SolidworksApi/macros
Answers 3
RJ
RJ 2021-04-08 Best Answer

Hi Byron,

It appears that the InsertedComponent has not been set. I would advise you to check if the InsertedComponent is not nothing before applying the transform. Kindly refer to the attached updated macro.

Hope it helps!!!

Rajat Jain

RJ
RJ 2021-04-08

Hi Byron,

It appears that the assembly was not activated correctly. AddComponent() API call works only when the requisite assembly is active. Please confirm this in your case.

Hope it helps!!!

Rajat Jain

BC
BC 2021-04-08

Rajat, thanks for the updated code. However, the assembly file opens, but no components are inserted as intended. 

When I add "set" to "boolstatus = swInsertedComponent.SetTransformAndSolve2(swTransform)", it then tells me that boolstatus needs to be an Object, but that will result in a type mismatch with SetTransformAndSolve. 

Adding "With" and "End With" on the ends of the boolstatus equality also yield the same run-time error.