You are on page 1of 18

Debugging Tips for SolidWorks API Programmers

Version 1.1

Written by: Keith Rice, Technical Director CADSharp LLC www.cadsharp.com Questions to ask yourself if youre stuck while debugging: Are you sure you have arguments correct? o For example, consider the following line. Note that no @ was appended after Point1@Origin, consequently the entity name was wrong and nothing was selected. swModel.Extension.SelectByID2 "Point1@Origin" & strCompName & "@" & strAssyName, _ "EXTSKETCHPOINT", 0, 0, 0, True, 1, Nothing, 0 Are you sure the API call does what you think it does? Are you sure you are using the correct API call? o For example, sometimes people get ISldWorks::ActivateDoc and ISldWorks::ActiveDoc confused. Are you sure that you arent trying to force the method do something it cannot do? o Example: if you rename a drawing sheet to Sheet1 but a sheet with that name already exists o Example: if you have two planar faces selected and try to apply a concentric mate

Error prevention tips: Type enumerations and member names in lowercase, so that when your cursor moves the next line, you can see if certain letters snapped to uppercase, indicating that the name is recognized. Use Intellisenses auto-complete If you use Find and Replace; be sure to thoroughly test your code afterward

Common VBA Errors The follow pages are an assortment of compile or run-time errors that may occur while programming SolidWorks using Visual Basic for Applications. Page 3 Compile error: variable not defined Page 4 "Run-time error '450': Wrong number of arguments or invalid property assignment" Page 5 "Run-time error '91': Object variable or With block variable not set" Page 6 "Run-time error '5': Invalid procedure call or argument" Page 7 "Run-time error '13': Type mismatch" Page 8 "Code execution has been interrupted" Page 9 "Run-time error '438': Object doesn't support this property or method" Page 10 "Run-time error '-2147417848 (80010108)': Automation error The object invoked has disconnected from its clients" Page 11 "Run-time error '98': A property or method call cannot include a reference to a private object, either as an argument or as a return value" Page 12 "Compile error: Method or data member not found" Page 13 "Run-time error '424': Object required" Page 14 "Compile error: Expected user-defined type, not project" Page 15 "Run-time error '-2147417851 (80010105)': Automation error The server threw an exception." Page 16 "Run-time error '445': Object doesn't support this action" Page 17 "Compile error: Can't find project or library" Page 18 Compile error: User-defined type not defined

Cause: You may have Option Explicit present and did not declare a variable

Cause: You probably mistyped something Example: swFaces = CircularEdgeCollection.Item(i).GetTwoAdjacentFaces(2) The two should not be in parentheses: swFaces = CircularEdgeCollection.Item(i).GetTwoAdjacentFaces2

Cause: You didnt Set the variable used on that particular line in particular, or the line that was supposed to Set that variable failed to do so. Example 1: If no part is open and you try to run this you will get the error on Set swCustPropMgr Dim swApp As SldWorks.SldWorks Dim swModel As SldWorks.ModelDoc2 Dim swCustPropMgr As SldWorks.CustomPropertyManager Sub main() Set swApp = Application.SldWorks Set swModel = swApp.ActiveDoc Set swCustPropMgr = swModel.Extension.CustomPropertyManager("") Debug.Print swCustPropMgr.Count End Sub Example 2: In this next example, swFace(i+1) should only be swFace(i); the 3rd element does not exist For i = 0 To 1 Set swFace(i) = swSelMgr.GetSelectedObject6(i + 1, -1) Set swSurface(i) = swFace(i + 1).GetSurface Next i Example 3: This error can also occur if a method requires an array of doubles and you declare the array as a variant. Example 4: This error can also occur when programming attributes. If the attribute definition already exists and fails to register, IAttributeDef::CreateInstance5 can fail, in which case calls to IAttribute members will cause this error. You may need to create an attribute definition with a different name or restart SolidWorks.

Example: Dim vFaces As Variant Dim i As Integer For i = 0 To collCircEdges.Count vFaces = collCircEdges.Item(i) Next i Cause: There is nothing in the collection, because the code earlier that added to the collection was commented out.

Cause: This can occur for many different reasons, such as a variable not getting set properly, or a method being run on an empty variable, or when you accidentally declare an object as the wrong type. Example 1: Set vEdges = swFace.GetEdges Should be vEdges = swFace.GetEdges Example 2: Or, if vCustPropNames is Empty: vCustPropNames = swCustPropMgr.GetNames For Each vName In vCustPropNames Debug.Print " " & vName Next vName

Example 3: Set swSurface1 = vFaces(0) Should be Set swSurface1 = vFaces(0).GetSurface Example 4: Dim swApp as SldWorks.SldWorks Dim swModel as SldWorks.SldWorks Sub main() Set swApp = Application.SldWorks Set swModel = swApp.ActiveDoc End Sub You will get a type mismatch error on Set swModel because you declared swModel as SldWorks.SldWork s and not SldWorks.ModelDoc2

Cause: If you are trying to traverse entities and you misused or left out the code that gets the next entity. Example: Set swLoop = swFace.GetFirstLoop While Not swLoop Is Nothing 'If Not swLoop.IsOuter Then ' swEdges = swLoop.GetEdges ' For i = 0 To UBound(swEdges) ' Set swCurve = swEdges(i).GetCurve ' If swCurve.IsCircle Then ' 'add to a circular face collection ' collCircEdges.Add swEdges(i) ' End If ' Next i 'End If swLoop.GetNext Wend The line in red should be "Set swLoop = swLoop.GetNext".

Cause: This can occur if the object was not set. This can also occur if you try using an API call that doesnt exist in the currently referenced SolidWorks type libraries. Example 1: Set swLoop = swFace.GetFirstLoop While Not swLoop Is Nothing ' Do something here swLoop = swLoop.GetNext -->Needs "Set" in front Wend Example 2: If swSelMgr.GetSelectedObjectType3(i, -1) = swSelectType_e.swSelEDGES Then vEdges(i) = swSelMgr.GetSelectedObject6(i, -1) End If Need to include Set in front of vEdges(i) Example 3: IAssemblyDoc::InsertNewVirtualPart became available in the SolidWorks 2010 type library. If you tried using this API call in a macro or add-in that is referencing earlier type libraries, youll get this error.

Cause: The pointer to an object was lost. (In other words, you lost access to that objects interface because the object reference no longer exists.) Example 1: Please see the macro examples involving safe entities on how to use safe entities to prevent an entity object pointer from being destroyed. Certain API calls will destroy object pointer, even though you may not have wanted that to occur. Example 2: Also, you may have accidentally destroyed the object pointer yourself in your code. In the following example, the IModelDoc2 pointer is going to be lost as soon as the document is closed. Then as soon as a member from the IModelDoc2 interface is called, the error will occur: Dim swApp As SldWorks.SldWorks Dim swModel As SldWorks.ModelDoc2 Sub main() Set swApp = Application.SldWorks Set swModel = swApp.ActiveDoc swApp.CloseDoc swModel.GetTitle swModel.ForceRebuild3 False End Sub Example 3: This can also occur in macro features in the rebuild routine, when an IBody2 member is used on an IBody2 pointer that doesnt exist anymore. You will need to re-obtain the IBody2 pointer.

Cause: This occurs when a modules instancing is on Private and not PublicNotCreatable. Frequently this will happen when a macro written on one computer is run on a different computer. The solution is to change the instancing from Private to PublicNotCreatable in the class modules properties.

Cause: You probably misspelled an API call.

Cause: You may have tried to set an object equal to an API call that returns an array, not an object. You may have also used the argument Null instead of Nothing. Example 1: Dim swApp As SldWorks.SldWorks Dim swDoc As SldWorks.ModelDoc2 Dim swPart As SldWorks.PartDoc Dim swBodies As SldWorks.Body2 Sub main() Set swApp = Application.SldWorks Set swDoc = swApp.ActiveDoc Set swPart = swDoc Set swBodies = swPart.GetBodies2(SwConst.swBodyType_e.swSolidBody, True) End Sub The line in red is incorrect. First, if you look in the API Help, you see that IPartDoc::GetBodies2 returns an array, not an object. Second, you do not use Set with an array. After the declaration for swBodies is renamed to vBodies and declared as type Variant, the second-to-last line should be corrected to this: vBodies = swPart.GetBodies2(SwConst.swBodyType_e.swSolidBody, True) Example 2: swFilletFeatData.AccessSelections swModel, Null The last argument should be Nothing. Null is not used in VBA to indicate that an object is nothing. Example 3: This can also occur if you have unrecognized references checked relating to controls. For example, you may get this with the Visible property of a control. Try unchecking control-related references and see if the problem disappears.

Cause: This can occur if you write Dim swApp as SldWorks during your variable declaration, rather than Dim swApp As SldWorks.SldWorks.

Cause: This can occur when an object needs to be passed as an argument, and Nothing is passed instead. This can also occur if you try to run a method on a lightweight component or feature that requires the component or feature to be fully resolved. This might also occur if IDisplayDimension::GetNext5 is used instead of IFeature::GetNextDisplayDimension when trying to traverse all dimensions in a feature, beginning with IFeature::GetFirstDisplayDimension. Example: Note: you need to select a face before running this code. Dim swApp As SldWorks.SldWorks Dim swModel As SldWorks.ModelDoc2 Dim swSelMgr As SldWorks.SelectionMgr Dim swLoop As SldWorks.Loop2 Dim swEdge As SldWorks.Edge Dim vLoops As Variant Dim vEdges As Variant Dim i As Integer Dim j As Integer Sub main() Set swApp = Application.SldWorks Set swModel = swApp.ActiveDoc Set swSelMgr = swModel.SelectionManager Set swFace = swSelMgr.GetSelectedObject6(1, -1) vLoops = swFace.GetLoops For i = 0 To UBound(vLoops) Set swLoop = vLoops(i) vEdges = swLoop.GetEdges For j = 0 To UBound(vEdges) Set swEdge = vEdges(j) swLoop.Select Nothing, True, Nothing Next j Next i End Sub

The Nothing in red needs to be changed to swEdge (without quotes).

Cause: This occurs when you use an API call that is not available. Example: If you use the API call IModelDoc2::Close. According to the API Help, This function is currently unavailable. Use ISldWorks::CloseDoc.

Cause: This can happen when a macro is run on a new computer. The SolidWorks Type libraries may not be loaded in correctly. To fix this, remove and re-add the references in ToolsReferences within the VB Editor, or copy all of the code into a new macro and run that macro.

Cause: This can occur if you try to implement a class that doesnt exist in the current type library. Example: The class PropertyManagerPage2Handler9 became available in SolidWorks 2012. If you had the SolidWorks 2011 type library referenced and tried to implement this class, you would get this compile error.

You might also like