Skip to content

Microsoft Visual Basic: Run-time Error ’91’

Article by Samony Riyaz created/updated October 5, 2019

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'

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.

  1. Ensure that you have access to the Macro file that is being run within SOLIDWORKS
  2. 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
  3. You will see a VBA Editor window, seen in the screenshot below:

    Sample Macro Code

    Sample Macro Code

  4. 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.

    Add a breakpoint to the code

    Add a break point to the code

  5. 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.
  6. The line should begin with a “Set OBJECT = ___________” Statement
    • This indicates that a variable was used without being declared.
  7. 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.

Posts related to 'Microsoft Visual Basic: Run-time Error ’91’'

Find Related Content by TAG:

Samony Riyaz

Want to learn SOLIDWORKS?

Take a training course from our team of Certified SOLIDWORKS Experts

Scroll To Top