Creating multivalue parameters in VBA

Creating multivalue parameters in VBA

Thomas.Long
Advocate Advocate
879 Views
6 Replies
Message 1 of 7

Creating multivalue parameters in VBA

Thomas.Long
Advocate
Advocate

Ok, so I'm trying to shift some of my stuff over from iLogic to VBA and I'm having some issues with multivalue parameter creation. The parameter.ExpressionList.SetExpressionList function is just endlessly crashing the program. No error messages, no warnings, even the debugger won't do anything. Just boom, program gone, every single time.

 

I know that my function is returning a new parameter so I'm a little unsure what is going on. 

 

Thank you

 

    Dim oParam As UserParameter
    
    Dim valList(4) As String
    valList(0) = "None"
    valList(1) = "Side A"
    valList(2) = "Side C"
    valList(3) = "Side A & C"
    
    Set oParam = Assembly_Functions.ParaCreation("LWSplits", "None", "Text")
    Call oParam.ExpressionList.SetExpressionList(valList)
'The ParaCreation function checks if a parameter exists and if it does not creates it with the defined type and starting value.
'The types defined for it are "Number", "Boolean", and "Text"
Function ParaCreation(sName As String, StartValue As String, oType As String) As UserParameter

    'Declaring document variables
    Dim oMyAssembly As AssemblyDocument
    Set oMyAssembly = ThisApplication.ActiveDocument
    
    Dim oMyParameter As UserParameters
    Set oMyParameter = oMyAssembly.ComponentDefinition.Parameters.UserParameters
    
    Dim oCompDef As AssemblyComponentDefinition
    Set oCompDef = oMyAssembly.ComponentDefinition
    
    Dim oParam As UserParameter

    'Create boolean for checking existence of parameter
    Dim blTag As Boolean
    blTag = False

    'Check through parameters to make sure parameter doesn't exist
    For i = 1 To oCompDef.Parameters.Count
    
        If oCompDef.Parameters(i).name = sName Then
            blTag = True
            Set oCompDef.Parameters(i).Value = StartValue
            Set oParam = oCompDef.Parameters(i)
            Exit For
        End If
    Next

    'Create parameter if it doesn't exist
    If blTag = False Then
        If oType = "Text" Then Set oParam = oMyParameter.AddByValue(sName, StartValue, UnitsTypeEnum.kTextUnits)
        If oType = "Number" Then Set oParam = oMyParameter.AddByExpression(sName, StartValue, "in")
        
        If oType = "Boolean" Then
            Dim boolValue As Boolean
            
            If StartValue = "True" Then
                boolValue = True
            Else
                boolValue = False
            End If
            
            Set oParam = oMyParameter.AddByValue(sName, boolValue, "Boolean")
        End If
    End If
    
    Set ParaCreation = oParam
    
End Function

 

0 Likes
880 Views
6 Replies
Replies (6)
Message 2 of 7

Sergio.D.Suárez
Mentor
Mentor

Hi, try to run the following macro. I made some changes to your initial code.
I have tried this macro and it worked for me.

Sub Create_Parameter()
   Dim oParam As UserParameter
    
    Dim valList(4) As String
    valList(0) = """None"""
    valList(1) = """Side A"""
    valList(2) = """Side C"""
    valList(3) = """Side A & C"""
    
    Set oParam = ParaCreation("LWSplits", "None", "Text")
    Call oParam.ExpressionList.SetExpressionList(valList)
End Sub

'The ParaCreation function checks if a parameter exists and if it does not creates it with the defined type and starting value.
'The types defined for it are "Number", "Boolean", and "Text"
Function ParaCreation(sName As String, StartValue As String, oType As String) As UserParameter

    'Declaring document variables
    Dim oMyAssembly As AssemblyDocument
    Set oMyAssembly = ThisApplication.ActiveDocument
    
    Dim oMyParameter As UserParameters
    Set oMyParameter = oMyAssembly.ComponentDefinition.Parameters.UserParameters
    
    Dim oCompDef As AssemblyComponentDefinition
    Set oCompDef = oMyAssembly.ComponentDefinition
    
    Dim oParam As UserParameter

    'Create boolean for checking existence of parameter
    Dim blTag As Boolean
    blTag = False

    'Check through parameters to make sure parameter doesn't exist
    For i = 1 To oCompDef.Parameters.Count
    
        If oCompDef.Parameters(i).Name = sName Then
            blTag = True
            oCompDef.Parameters(i).Value = StartValue
            Set oParam = oCompDef.Parameters(i)
            Exit For
        End If
    Next

    'Create parameter if it doesn't exist
    If blTag = False Then
        If oType = "Text" Then Set oParam = oMyParameter.AddByValue(sName, StartValue, UnitsTypeEnum.kTextUnits)
        If oType = "Number" Then Set oParam = oMyParameter.AddByExpression(sName, StartValue, "in")
        
        If oType = "Boolean" Then
            Dim boolValue As Boolean
            
            If StartValue = "True" Then
                boolValue = True
            Else
                boolValue = False
            End If
            
            Set oParam = oMyParameter.AddByValue(sName, boolValue, "Boolean")
        End If
    End If
    
    Set ParaCreation = oParam
    
End Function

 Hope this helps to solve your problem. Cheers!


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

0 Likes
Message 3 of 7

Thomas.Long
Advocate
Advocate

Nah, I tried running it and got the same problem. It's still just crashing there. 

 

Can I ask what version of inventor you're using? I'm on 2015. I saw that as a fix for this back in 2011 but wasn't able to get it to work

0 Likes
Message 4 of 7

Sergio.D.Suárez
Mentor
Mentor

I am using inventor 2020.
Here is a video of the process on my pc


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

0 Likes
Message 5 of 7

Sergio.D.Suárez
Mentor
Mentor

Try the following code, in the past I have had some problems with the character ", so sometimes it is necessary for the formula that replaces the character value.
Another change placed in this code, is the complete function "SetExpressionList" with the parameters false and -1.

 

Sub Create_Parameter()
   Dim oParam As UserParameter
    
    Dim valList(4) As String
    valList(0) = Chr(34) & "None" & Chr(34)
    valList(1) = Chr(34) & "Side A" & Chr(34)
    valList(2) = Chr(34) & "Side C" & Chr(34)
    valList(3) = Chr(34) & "Side A & C" & Chr(34)
    
    Set oParam = ParaCreation("LWSplits", "None", "Text")
    Call oParam.ExpressionList.SetExpressionList(valList, False, -1)
End Sub

'The ParaCreation function checks if a parameter exists and if it does not creates it with the defined type and starting value.
'The types defined for it are "Number", "Boolean", and "Text"
Function ParaCreation(sName As String, StartValue As String, oType As String) As UserParameter

    'Declaring document variables
    Dim oMyAssembly As AssemblyDocument
    Set oMyAssembly = ThisApplication.ActiveDocument
    
    Dim oMyParameter As UserParameters
    Set oMyParameter = oMyAssembly.ComponentDefinition.Parameters.UserParameters
    
    Dim oCompDef As AssemblyComponentDefinition
    Set oCompDef = oMyAssembly.ComponentDefinition
    
    Dim oParam As UserParameter

    'Create boolean for checking existence of parameter
    Dim blTag As Boolean
    blTag = False

    'Check through parameters to make sure parameter doesn't exist
    For i = 1 To oCompDef.Parameters.Count
    
        If oCompDef.Parameters(i).Name = sName Then
            blTag = True
            oCompDef.Parameters(i).Value = StartValue
            Set oParam = oCompDef.Parameters(i)
            Exit For
        End If
    Next

    'Create parameter if it doesn't exist
    If blTag = False Then
        If oType = "Text" Then Set oParam = oMyParameter.AddByValue(sName, StartValue, UnitsTypeEnum.kTextUnits)
        If oType = "Number" Then Set oParam = oMyParameter.AddByExpression(sName, StartValue, "in")
        
        If oType = "Boolean" Then
            Dim boolValue As Boolean
            
            If StartValue = "True" Then
                boolValue = True
            Else
                boolValue = False
            End If
            
            Set oParam = oMyParameter.AddByValue(sName, boolValue, "Boolean")
        End If
    End If
    
    Set ParaCreation = oParam
    
End Function

 


Please accept as solution and give likes if applicable.

I am attaching my Upwork profile for specific queries.

Sergio Daniel Suarez
Mechanical Designer

| Upwork Profile | LinkedIn

0 Likes
Message 6 of 7

Thomas.Long
Advocate
Advocate

I'll give it a shot tomorrow and let you know how it goes. Thank you for all the help!

0 Likes
Message 7 of 7

Thomas.Long
Advocate
Advocate

Sorry, it's still causing the same crashing error

0 Likes