Free PDF: 23 VBA Errors and How To Fix Them

Blog 3.6.2012 No Comments

Download the free PDF here

If you’ve done any significant amount of programming with the SolidWorks API, no doubt you’ve run into compile or run-time errors. In my thousands of hours working with the API, here are the 23 I encounter the most:

  • “Compile error: variable not defined”
  • “Run-time error ’450′: Wrong number of arguments or invalid property assignment”
  • “Run-time error ’91′: Object variable or With block variable not set”
  • “Run-time error ’5′: Invalid procedure call or argument”
  • “Run-time error ’13′: Type mismatch”
  • “Code execution has been interrupted”
  • “Run-time error ’438′: Object doesn’t support this property or method”
  • “Run-time error ‘-2147417848 (80010108)’: Automation error The object invoked has disconnected from its clients”
  • “Run-time error ’98′: A property or method call cannot include a reference to a private object, either as an argument or as a return value”
  • “Compile error: Method or data member not found”
  • “Run-time error ’424′: Object required”
  • “Compile error: Expected user-defined type, not project”
  • “Run-time error ‘-2147417851 (80010105)’: Automation error The server threw an exception.”
  • “Run-time error ’445′: Object doesn’t support this action”
  • “Compile error: Can’t find project or library”
  • “Compile error: User-defined type not defined”
  • “Run-time error ‘-2147417848 (80010108)’: Method ‘IDisplayWireFrameXOR’ of object ‘IBody2’ failed”
  • “Compile error: ByRef argument type mismatch”
  • “Compile error: The code in this project must be updated for use on 64-bit systems. Please review and update Declare statements and then mark with the PtrSafe attribute.”
  • “Could not load some objects because they are not available on this machine.”
  • “Run-time error ‘53’: File not found:
  • “Compile error: Expected variable or procedure, not project”
  • “Compile error: Object library feature not support”

Figuring out what these errors mean and how to fix them can be a real challenge. That’s why I created a free PDF, VBA Debugging Tips for SolidWorks API Programmers, that addresses each of these errors—their causes AND their solutions. This PDF also covers basic debugging tips that will be helpful for those new to SolidWorks API programming.

Let me address just one of these right now. The third error listed is “Run-time error ’91′: Object variable or With block variable not set”. This usually occurs when you are trying to use an API call belonging to an interface that you have not yet accessed. So for example, try typing this in your VB Editor and running it:

Dim swApp As SldWorks.SldWorks
    Sub main()
    swApp.SendMsgToUser "Hello world!"
End Sub

You should get the aforementioned error:

The error is occurring because the swApp object variable was never set. Consequently we can remedy the problem rather easily by simply adding one line to our macro:

Dim swApp As SldWorks.SldWorks
Sub main()
    Set swApp = Application.SldWorks
    swApp.SendMsgToUser "Hello world!"
End Sub

Likewise, you’ll also get the same error if you leave out the Set keyword. (In Visual Basic for Applications, assigning a reference to an object variable requires the Set keyword. This isn’t the case in other languages, however.)

Sometimes you will get this error message in situations where the solution is less obvious. Chances are you doing one of the following:

  1. You accidentally killed the pointer you were intending to use. For example, if you closed out a model and then tried using the IModelDoc2 pointer to that document that you had previously obtained.
  2. You are using a method incorrectly, so that the interface you thought was going to be returned was never actually returned. For example, if you use IAssemblyDoc::AddMate3 to create a concentric mate between two planar faces, the API call will obviously fail to return the IMate2 object. Therefore, when you try to later use an IMate2 member with the variable that you thought contained an IMate2 object, you get this error.

In conclusion, I think we can all agree that the least enjoyable aspect of programming is debugging. Reduce the time you’ll spend debugging by using my free SolidWorks API debugging PDF and also by taking advantage of our free SolidWorks API training. Lastly, please let me know in the comments below of any other errors that you think I should include in the PDF.

Happy coding!
Keith

Want to keep up with future CADSharp.com content, webinars, and special offers? Sign up for our newsletter.


Questions and Comments

You must be logged in to post a comment.