Object of type 'System.UInt32' cannot be converted to type 'Inventor.CommandType
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report
I'm trying to simply create parameters in a list of parts with error checking. Every time the macro is run, it creates these variable, so if I run it more than once it will create new parameters with "_1", "_1_2", etc appended to the end. My code should delete all old parameters and create new ones every time it runs. I've spent several hours trying to debug this, but I can't figure out what's wrong. Below is my full code. Note that I have neglected %Path because it contains sensitive company information. I'm new to programming and recently started using VBA so please excuse bad coding practices.
Option Explicit Sub CreateCoverParameters() Dim sSectionPath As String sSectionPath = "C:\Work\%Path\" 'Assign cover file names to array elements Dim sCover(2) As String sCover(0) = sSectionPath & "1343565.ipt" sCover(1) = sSectionPath & "1343322.ipt" sCover(2) = sSectionPath & "1343321.ipt" 'MsgBox sCover(2) 'MsgBox UBound(scover) 'Iterate and create parameters in each cover 'Declare user parameters Dim sUserParams() As String sUserParams = Split("lPatternLength,lPatternLengthShort", ",") 'Declare iteration variables Dim iCoverCount As Integer Dim oCoverParam As parameter Dim iParamTest As Integer Dim sParamTest As String Dim iParameterCount As Integer Dim iUserParamCount As Integer For iCoverCount = 0 To UBound(sCover) 'Test if parameter is duplicate and delete iParameterCount = ThisApplication.Documents.ItemByName(sCover(iCoverCount)). _ ComponentDefinition.Parameters.UserParameters.Count For iParamTest = 1 To iParameterCount 'MsgBox ThisApplication.Documents.ItemByName(sCover(iCoverCount)). _ ComponentDefinition.Parameters.UserParameters.Item(iParamTest).Name sParamTest = ThisApplication.Documents.ItemByName(sCover(iCoverCount)). _ ComponentDefinition.Parameters.UserParameters.Item(iParamTest).Name For iUserParamCount = 0 To UBound(sUserParams) If InStr(1, sParamTest, sUserParams(iUserParamCount)) > 0 Then ThisApplication.Documents.ItemByName(sCover(iCoverCount)). _ ComponentDefinition.Parameters.UserParameters.Item(iParamTest).Delete End If Next Next 'Create parameter For iUserParamCount = 0 To UBound(sUserParams) Set oCoverParam = ThisApplication.Documents.ItemByName(sCover(iCoverCount)).ComponentDefinition. _ Parameters.UserParameters.AddByExpression(sUserParams(iUserParamCount), 0, "in") Next Next End Sub
When I run this, I get
"Run-time error '-2147024809 (80070057)':
Object of type 'System.UInt32' cannot be converted to type 'Inventor.CommandTypesEnum'."
The weird thing is that if I run and debug, the first portion of pink text is highlighted sometimes. Other times, the second portion of pink text is highlighted. Also, this code has worked perfectly before, but now I can't get it to work. Edit: It works perfectly when I delete all related user parameters then run it, so I guess the parameter creation part is working but the "error checking" part is not, which kind of defeats the purpose.
I first thought maybe I had declared sParamTest with the wrong data type, but checking the object browser, the Name property is, indeed a string. Then I thought maybe I made a mistake in the InStr function, but everything looks correct.
Any help is greatly appreciated.