By HawkRidge Systems Engineering Team
There is nothing more frustrating than running into an error message in the process of writing or running a macro and not knowing what the issue is or how to even start fixing it. Hopefully this post will demystify the errors a bit and get you pointed in the right direction to solve the problem.
There are two main types of errors that you will run into when writing a SolidWorks macro: compile errors and runtime errors. Compile errors are usually caught as you’re writing the code. When you hit the “Enter” key to go to the next line, the VBA environment will pop up a message that looks something like this:
The offending line of code is highlighted in red. This type of error is a result of some incorrect programming syntax. The VBA language has many requirements about what has to be enclosed in parentheses, how loops (For loops, While loops, etc.) and If statements are structured, and much more. Breaking any of these rules will result in a compile error.
If you’re completely new to programming, picking up a book like VBA For Dummies will be a great help. If you’re already familiar with programming, doing some quick online searches to research the particular syntactical quirks of VBA should suffice.
As you spend more time with VBA in the SolidWorks API environment, the headache and mystery associated with these types of errors will greatly lessen. Note that some types of compile errors pop up after the macro is run but before the code actually begins executing:
These are still syntactical errors, but they aren’t necessarily tied to the VBA language itself. These two examples both have to do with errors in the definition of the parameters of a method. The “Argument not optional” error indicates that one or more of that method’s parameters are missing. “Wrong number of arguments” usually means that there are more parameters than the method asks for. The number of parameters for each method is defined by SolidWorks in the API, and all parameters are required. The Help File is a good resource for figuring out what parameters are defined, what data type is needed, and how many there are.
Runtime errors are caught only when you try to run the macro. The code will execute part of the way through and stop at the error, popping up a message like this:
If you click the “Debug” button, the program will highlight the line of code that produced the error so you can target your efforts to fix it:
Here are a few common runtime errors and how to resolve them:
1. Object or with block variable not set
This is most commonly due to trying to assign an object to a variable without using the “Set” keyword. Non-object variables are assigned without the “Set” keyword, as follows:
Dim i As Integer
i = 10
VBA requires that if a variable has been declared as an object of any kind, it must be assigned as follows:
Dim swVar As Object
Set swVar = Interface.Accessor
If this error pops up, check to make sure that you’ve used the “Set” keyword whenever assigning an object to a variable. Additionally, make sure that the variable has actually been set to an object before trying to use it.
2. Object doesn’t support this property or method
This error means that you’ve tried to access a property or use a method that doesn’t exist in the API. Many times this simply means a typo – check your spelling and try again. Otherwise, you may be trying to use a method that actually belongs to another interface or incorrectly recalling the name of a method – for example, trying to use CreateCircleByDiameter (which doesn’t exist) instead of CreateCircleByRadius (which is a method belonging to the ISketchManager interface). Usually, a quick Help File search will allow you to resolve the problem. 3. Type mismatch
You will see this error if you try to assign the wrong type of data to a variable that you’ve declared. For example, the following would produce this error:
Dim i As Integer
i = “Text string.”
The assignment of the variable to some text (String) doesn’t match the type of data (Integer) that the program was told to expect. You will also see this error if you try to use the wrong data type for a particular parameter in a function.
For example, the CreateCircleByRadius function requires three parameters to define the X, Y, and Z locations of the center of the circle and a fourth to define the desired radius. All four parameters must be of the “Double” (an 8-byte floating point number) data type. If you try to give the function String data instead of Double data, you will see this error. Again, a quick Help File search for the method you’re trying to use will help clarify which data type is expected for each parameter.
If you get stuck on a particular error, don’t give up! Read the error message carefully, check the Help File, search the internet, call technical support, or use whatever other resources you have available to break the problem down.
If you’re interested in the SolidWorks API, make sure to check out the recorded webinar: “Introduction to the SolidWorks API”.
See the previous posts from this series: Getting Started with the SolidWorks API - Part 1
SolidWorks API Building Blocks - Part 2
SolidWorks API Building Blocks - Part 3
Using the SolidWorks API Help File - Part 5