Fix “Compile error : Can’t find project or library”

Blog 4.16.2013 1 Comment

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, open up the macro in the Visual Basic Editor. Go to Tools–References, which displays a list of the libraries your macro is referencing. Do you see “MISSING” before any of the checked listings at the top? (Example) If yes, then try this first set of instructions. Otherwise, skip to the second set of instructions.

  1. Uncheck the missing references.
  2. Click OK in the References dialog box
  3. Re-open the References dialog by going to Tools–>References.
  4. Re-check the references you unchecked earlier. (They should still be listed near the top of the list.)
  5. Test out the macro now. If the problem persists, you can return to the References dialog box and try navigating to the location of the .tlb or .dll, which is usually in a folder like C:\Program Files\SolidWorks Corp\SolidWorks. If this still doesn’t fix the problem, to the next set of instructions.

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.


One Comment

  1. vzikram

    thanks….worked for me 1st step…

Questions and Comments

You must be logged in to post a comment.