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:
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.
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
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.
At the computer where the error is occurring:
- Open up the macro in the Visual Basic Editor.
- Go to Tools–References. If this is grayed out then the macro is still running. Stop it by going to Run–>Reset.
- Locate the references with “MISSING” in the names. (Example) If you do not see any, then skip to the second set of instructions below.
- Uncheck the missing references.
- 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.
- Click OK to exit the references dialog.
- 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.)
- Leave the VB Editor open and return to SolidWorks. Create a new macro by going to Tools–>Macro–>New.
- Back in the VB Editor, both the problematic macro and the new macro should be visible in the Project Explorer in the top left.
- 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.
- One by one, drag all of the modules from the problematic macro into the new macro’s project name.
- 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,
Did you find this post helpful? Keep up with future CADSharp.com content, webinars, and special offers by signing up for our newsletter.
thanks….worked for me 1st step…