You’ve written your first “serious” SolidWorks API macro. You’re ready to share with your co-workers. You fire up your email, attach the macro, and click Send. Expecting an avalanche of compliments, you instead get several responses from your co-workers telling you that the macro doesn’t run. “Doesn’t run?! How can that be? It runs fine on my computer!” You ask for a screenshot of the error and this is what they send you:

Compile error: can't find project or library

Has this ever happened to you? Maybe not in this exact scenario, but the infamous “Compile error: Can’t find project or library” error message eventually befalls every API programmer trying to share their VBA handiwork with others. Fortunately, it isn’t that hard to fix.

The Cause

When you write code with the SolidWorks API, you constantly use what are called “API calls”. These are the functions you use in your code to execute commands in SolidWorks. Visual Basic for Applications, however, can’t make heads or tails of these API calls unless your code references the appropriate libraries. By libraries I mean the .dll and .tlb files created by SolidWorks Corporation that define each SolidWorks API call. Some of the more common ones include:

  • SolidWorks 20XX Type Library
  • SolidWorks 20XX Commands type library
  • SolidWorks 20XX Constant type library
  • SolidWorks 20XX Extensibility type library
  • Etc.

You can see this list if, while in the VB Editor, you go to Tools–>References. If you click on one of these listings, you’ll also see the file path for the type library. When you create a new macro on your computer, the macro looks at these paths for the references. The problem occurs when you transfer that macro to another computer that has these references in a different location. If your macro can’t locate a reference, you may see the word “MISSING” before the missing reference, but not necessarily.

The Solution

At the computer where the error is occurring:

  1. Open up the macro in the Visual Basic Editor.
  2. Go to Tools–References. If this is grayed out then the macro is still running. Stop it by going to Run–>Reset.
  3. Locate the references with “MISSING” in the names. (Example) If you do not see any, then skip to the second set of instructions below.
  4. Uncheck the missing references.
  5. For your version of SOLIDWORKS, locate the references with the equivalent names and check them. If the missing references correspond to the version you are using, then first click OK on the References dialog and then re-open the references dialog.
  6. Click OK to exit the references dialog.
  7. Test the macro.

In this second set of instructions, you will copy the contents of the problematic macro to a brand new macro created on this computer. (This is the method demonstrated in the video at the beginning of the post.)

  1. Leave the VB Editor open and return to SolidWorks. Create a new macro by going to Tools–>Macro–>New.
  2. Back in the VB Editor, both the problematic macro and the new macro should be visible in the Project Explorer in the top left.
  3. Delete out the existing module in the new macro by right clicking on it and choosing Remove . Click No if you are asked whether you want to export the module.
  4. One by one, drag all of the modules from the problematic macro into the new macro’s project name.
  5. Test the new macro on this computer. You should not encounter an error.

Finally, don’t forget that this is just one of the many compile or run-time errors you can run into while programming with the SolidWorks API using VBA. Our FREE SolidWorks API Debugging Tips PDF contains descriptions and solutions for 14 other common errors. Grab it now and keep it handy—you won’t regret it!

Conquering compile errors,
Keith

Did you find this post helpful? Keep up with future CADSharp.com content, webinars, and special offers by signing up for our newsletter.