Microsoft Visual Basic: Run-time Error ’91’
Article by Samony Riyaz updated October 5, 2019
Article
Ever encountered the Microsoft Visual Basic error “Run-time Error ’91’: Object variable or With block variable not set” when opening a SOLIDWORKS file? Or if you’re creating a new SOLIDWORKS Macro, and while running it you see this pop up?

Microsoft VB Run-time Error ’91’
Lucky for you, this is a very common VB error that’s encountered and it’s not too difficult to determine where the issue lies.
Lets look at the steps that one needs to take to resolve this VB error.
- Ensure that you have access to the Macro file that is being run within SOLIDWORKS
- Launch up the .swp file in Edit mode. This can be done via Tools > Macro > Edit > Browse to the macro location OR through your macro toolbar > Edit > Browse to the macro location
- You will see a VBA Editor window, seen in the screenshot below:
- To identify where the error lies, you would need to STEP through the code. This can be done by placing BREAKPOINTS in the code by selecting the grey section next to the line as seen below.
- Now press the key ‘F8’ on your keyboard to STEP through the code until a line fails and gives you the Run-time Error.
- Make note of the line number resulting in that error.
- The line should begin with a “Set OBJECT = ___________” Statement
- This indicates that a variable was used without being declared.
- To address this issue, declare the OBJECT variable above the Set line using the “Dim OBJECT As SldWorks.OBJECT”
- Example: the Statement that errors out could be “Set Part = swApp.NewDocument….”
- The correct declaration for that would be “Dim Part As SldWorks.ModelDoc2”
To ensure that you have the correct object type, reference the SOLIDWORKS API Web Help for detailed information on the various commands and enumerations available.
Learn more about SOLIDWORKS Macros/API
Javelin provides SOLIDWORKS VB for Applications and SOLIDWORKS API training courses, which you can take live online, or in one of our Canadian classrooms. Check our schedule to learn more.
Related Links
Get Certified SOLIDWORKS Services from Javelin
Javelin Experts can help you to: