Getting Errors when using downloaded Macros?

Errors when running downloaded SolidWorks macros (Visual Basic 6) can be caused by many different issues, depending on the macro and which version of SolidWorks being used.  Here are some general points that may help.

Reference Libraries
Check reference libraries associated with the macro. These can be accessed by opening the macro through SolidWorks API editor (edit macro function), then going to pulldown Tools>Reference Libraries. If any of the listed libraries have the word MISSING in front of them, remove their check mark. You may need to replace it with a similarly named reference library that is included on your system. For example, if “SolidWorks 2008 Object Library” is missing and you are running SolidWorks 2007, then look for and activate the library named something like “SolidWorks 2007 Object Library”. SolidWorks may be able to do this automatically, but on many computer systems, you will need to manually make this edit, particularly if using a macro made on a version of SolidWorks newer than you are using.

Conversely, errors can be caused by having conflicting libraries loaded at the same time (i.e., having too many libraries).  No errors will appear in the reference library list.  If you are certain the correct library is loaded already, trying removing other SolidWorks related libraries one by one until the issue is resolved.

Also, the correct reference library may not be loaded at all. Research the line of code causing the error to make sure the correct reference library is loaded to support those instructions.

Additionally, if a macro was made on a newer version of SolidWorks than what you are currently using, its instructions may not be supported at all on the system running the older version of SolidWorks. In this case, you will need to find an equivalent API method for the older SolidWorks version, if one exists.

If the macro does not use Forms, a quick way to make sure you are using current reference libraries is to simply cut and paste the complete code from the existing macro file to a new one created fresh on your system. Often, this will fix any mismatched reference libraries, but it might still require research to make sure all the necessary reference libraries are loaded for that macro.

Sub-Modules
When executing the macro, make sure you specify the “Main” sub-module as the starting point. This is the section of code following the line “Sub Main ()”. Most macros are set up with Main as their starting sub-module. This is usually a fix if you are having issues associating your macro with an icon on a Toolbar, assigning a shortcut key, or trying to execute the macro from within another macro.

If the macro does not have a Main sub-module, then you may need to study the macro to discover which sub-module is meant as the starting point.

Copying code from online or plain text source
Sometimes when code appears in plain text, such as in a .txt file or from a forum online, it may not be formatted properly. Some of the lines may be too long for the plain text source, so they are line broken. This will disrupt the code, as SolidWorks API will misunderstand the instructions. When copy and pasting code from a plain text source, make sure to go over the code to insure all the lines are properly reconstructed by removing erroneous line breaks.

Sources for help
If issues are still occuring, contact the author(s), if available. Also, make sure to read over the material and examples provided in the SolidWorks API Help file included with SolidWorks. If those do not completely solve the issue, then search over various online forums dedicated to SolidWorks and SolidWorks API support. Many of these forums are listed under Forum Links.  If the search doesn’t resolve the issue, then post a question on those forums detailing the issue and requesting help from other SolidWorks users. You may also contact your VAR, as the issue you are having may actually be a bug or known issue in the SolidWorks API system itself.

Book Review: SolidWorks 2008 API

SolidWorks 2008 API Programming & Automation is recent self-published book by Luke Malpass, founder of AngelSix.  (You will not find his book on Amazon.)  Mr. Malpass wrote this book to fill an apparent gap in guide books about SolidWorks API.  The last book that I know of written specifically on this topic was Automating SolidWorks 2006 using Macros by Mike Spens, published way back in 2005.  Like that book, Mr. Malpass’ new book is spiral bound.

Mr. Malpass states in his introduction that he wrote his book to share his “ways of thinking and programming and hopefully to enlighten at least one other person’s day.”  Much of the book is written in this same friendly manner.  At times, the book is not written in a way that some would consider professional.  Yet, he gets his points across clearly.  The book is accessibly readable by experienced and novice programmers alike. 

The first chapter is correctly labelled “The Basics”.  The first section gets right into the task at hand. The lesson teaches how to make and use recorded macros.  The book next jumps right into writing macros from scratch.  It takes a step by step approach that reveals what code is necessary.  It explains each line in Mr. Malpass’ ever present friendly style. 

As one reads through the book, they will find code examples being built and explained piece by piece in both C# and VBA.  These macros are real world examples that the reader may find immediately useful.  Instructions are so clear that many will be able to apply the demonstrated code in their own macros.

One good example of this is found in the first chapter.  Right near the beginning of the book, Mr. Malpass provides instructions on how to make a macro that save each drawing sheet as an individual DXF File automatically.  This example is great because this question actually comes up a lot in the online message forums.

Many more important topics are extensively covered.  These include working with selected objects, implementation of Property Manager Pages, traversing through each type of document, Custom Property Manager, working with drawings, use of forms, and how to create and use Add-ins (something of special interest to many).  Additionally, all macro examples within the book are included in completed form on a CD. 

Of course, the book covers both C# and VBA code examples.  However, it also often talks extensively about VB.NET.  I found the instructions regarding VB.NET to be distracting, especially since few VB.NET examples appear in the book except for one section written specifically for it.  Granted, VB.NET and VBA are very similar.  However, in my opinion, differences are enough to warrant specific examples for each language.  It should be noted that some VB.NET examples are provided on the CD.  It seems to me that this book may have been better served if it focused on VB.NET and VBA instead of C# and VBA.  C# may have been better handled within its own book.  This would’ve allowed for more room in the book for more specific examples on even more topics, such as more information on how to read and utilize data from external files or methods specifically addressing the handling of BOM’s. 

One minor point of concern I found is within statement of ownership by the author.  It’s a boiler plate “all rights reserved” statement that limits reproduction or distribution without written permission of the publisher.   As this applies to the book itself, I stand by such statements.  However, in my view, it is not practical to place such limitations on code examples.  Doing so kind of defeats the purpose of the book.  That is not to say an author should not hold control over their works, but that if an author chooses to provide examples on how to do something, limitations should not be placed on the examples themselves.   (SolidWorks Corp makes a statement in their API Help that grants permission for the unlimited use of their examples.)

That aside, my opinion is that this book is great for anyone with programming exposure and who wishes to learn how to apply that experience directly to SolidWorks.  It is not meant for the person that has never attempted to program, unless that person is a brave soul willing to dive right in to the process.  I believe this book will be useful for people from novices to those of advanced programming skill.  It may also be a good reference guide to professionally trained expert programmers for SolidWorks specific programming possibilities and requirements.  I recommend this book.

Disclaimer:  This review is unsolicited by the book’s author.  It is based on use of product that I purchased at full price.  I receive no compensation for referrals.

If you are interested in SolidWorks 2008 API Programming & Automation, it may be purchased here: AngelSix.com.