Programming novice here trying to create his second macro. I keep getting a run-time error 91 when I test my code. I've been reading online in every forum I can find to try and figure out what I am doing wrong but can't figure it out. I know it is happening at this line approximately 2/3 of the way down:
Set inputPoints(inputPointCount) = selectedObj
I have debugged everything except for this one line. Can anyone help?
I've attached my part I created specifically to test. For reference I am using Inventor 2022, build 153, release 2022.0.1
Sub ExportWorkPointsV2()
'How to use: create your axes in this order: 1st sraight section, 1st bend, 2nd straight, 2nd bend... until final straight section.
'Create a workpoint for the start point and end point. Select all axes and the 2 points, run the macro. Macro will create points at
'each intersection of axis, place them in order, create a UCS using points 1-3, offset the points by the UCS, measure the distance
'of the bend axis to a straight axis, save that as a radius, export the XYZ and R of each point to an excel sheet.
Dim ready As Long
ready = 1
If ready = 0 Then
MsgBox "This macro is not ready yet"
Exit Sub
End If
Dim partDoc As PartDocument
Dim partCompDef As PartComponentDefinition
If ThisApplication.ActiveDocumentType = kPartDocumentObject Then
Set partDoc = ThisApplication.ActiveDocument
Dim partDef As PartComponentDefinition
Set partDef = partDoc.ComponentDefinition
Else
MsgBox "A part must be active"
Exit Sub
End If
Dim points() As WorkPoint
Dim inputPoints As WorkPoint
Dim axes() As WorkAxis
Dim axisCount As Long
axisCount = 0
Dim inputPointCount As Long
inputPointCount = 0
If partDoc.SelectSet.Count > 0 Then
ReDim axes(partDoc.SelectSet.Count - 1)
' ReDim inputPoints(partDoc.SelectSet.Count - 1)
MsgBox "Number of selected objects: " & Format(partDoc.SelectSet.Count, "0")
Dim selectedObj As Object
For Each selectedObj In partDoc.SelectSet
' MsgBox "Type of object is: " & TypeName(selectedObj)
If TypeOf selectedObj Is WorkAxis Then
Set axes(axisCount) = selectedObj
axisCount = axisCount + 1
'MsgBox "Axis found, axisCount = " & Format(axisCount, "0")
End If
If TypeOf selectedObj Is WorkPoint Then
' MsgBox "Type of object is: " & TypeName(selectedObj)
inputPointCount = inputPointCount + 1
MsgBox "Input point found, inputPointCount = " & Format(inputPointCount, "0")
End If
Next
' ReDim Preserve axes(axisCount - 1)
Else
MsgBox "Nothing selected. Please select all of your axes, your start point, and your end point, then try again"
Exit Sub
End If
MsgBox "Number of axes: " & Format(axisCount, "0") & " number of points: " & Format(inputPointCount, "0")
Dim rad As Double
rad = ThisApplication.MeasureTools.GetMinimumDistance(axes(0), axes(1)) / 2.54
MsgBox "Radius of 1st bend is: " & Format(rad, "0.000") & " in"
' getminimumdistance(axes(0),axes(1))
' Set points(0) = WorkPoint.SetByTwoPoints(axes(0), axes(2))
End Sub
Solved! Go to Solution.
Solved by J-Camper. Go to Solution.
VBA doesn't like you using a 'variable' to set how many items are to be in an Array object when you create it, and doesn't like you using a 'variable' to specify which item in an Array you are trying to read/write to. Because of this, I opted for using an ArrayList object, instead of an Array. But to use the ArrayList, I had to make sure the 'Reference' was turn on that lets VBA recognize that ArrayList type of object. I can't remember exactly which reference it was, but I think it was the one called "mscorlib.dll", and its path (on my PC) was at:
If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.
If you want and have time, I would appreciate your Vote(s) for My IDEAS :light_bulb:or you can Explore My CONTRIBUTIONS
Wesley Crihfield
(Not an Autodesk Employee)
Hello @Anonymous ,
I don't see the line you mention anywhere in the code you posted:
Set inputPoints(inputPointCount) = selectedObj
But I do see a ReDim line commented out:
' ReDim inputPoints(partDoc.SelectSet.Count - 1)
You are not declaring the "inputPoints" object as an array Object:
Dim points() As WorkPoint 'set as array
Dim inputPoints As WorkPoint 'NOT set as array
Dim axes() As WorkAxis 'set as array
Which might be all you need if the other array Objects are working as expected. I say this simply because I always opt for an ObjectCollection or a List(Of ObjectType) when gathering a group of Objects. [I have never tried to set a WorkPointObject as an array, but if it works then cool]
I don't have 2022 Inventor so I couldn't test your part, but let me know if you are still experiencing issues and I'll try to help
Thank you, you showed me what I did wrong. I forgot to add paranthesis after my inputPoints when I created them. Adding the parenthesis and uncommenting my redim fixed my issue!
Can't find what you're looking for? Ask the community or share your knowledge.