Inventor VBA: Change TYPE of Custom iproperties.

Inventor VBA: Change TYPE of Custom iproperties.

tonythm
Advocate Advocate
2,131 Views
7 Replies
Message 1 of 8

Inventor VBA: Change TYPE of Custom iproperties.

tonythm
Advocate
Advocate

Dear All

 

I have issue with type of custom iproperties available.

I want change type Text into Date, or Number. 

Could you please help me write the VBA code for this.

 

Thanks,

 

Thong

 

0 Likes
Accepted solutions (3)
2,132 Views
7 Replies
Replies (7)
Message 2 of 8

JhoelForshav
Mentor
Mentor

Hi @tonythm 

The property type depends on the type of object you assign as its value.

For example, say yoy have a custom text property named "test".

If you run this macro:

 

Sub test()
Dim oProp As Property
Set oProp = ThisApplication.ActiveDocument.PropertySets.Item("Inventor User Defined Properties").Item("test")
Dim oDate As Date
oDate = Now
oProp.Value = oDate
End Sub

 

The type will change to date, simply because the vaule is set to an object of type Date.

Message 3 of 8

WCrihfield
Mentor
Mentor

These links will give you a pretty good idea of how to check data type, and convert to the data type you want.

Data type summary

VarType function  

Type conversion functions 

If this helps, please click 'Likes' or 'Accept As Solution'.

Wesley Crihfield

EESignature

(Not an Autodesk Employee)

Message 4 of 8

tonythm
Advocate
Advocate

Hi @JhoelForshav

 

I have example below: I use excel vba file connect to Inventor to input value.

When I setup item type below on inventor:

Item A is Text

Item B is Number

Item C is Text

Item D is Date

And then I input data from excel same type, but all type that I was setup on Inventor change into Text.

I don't know what went wrong in my code.

If you know, please help me with this code.

 

Sub GetProperties()

Dim oWkbk As Workbook
Set oWkbk = ThisWorkbook

Dim oSheet As Worksheet
Set oSheet = oWkbk.ActiveSheet

Dim sSheetName As String
sSheetName = "Sheet1"

Dim sParamRange As String
sParamRange = "A1:A6"

Dim oInvApp As Inventor.Application
Set oInvApp = GetObject(, "Inventor.Application")

Dim oDoc As Document
Set oDoc = oInvApp.ActiveDocument

Dim oPropsets As PropertySets
Set oPropsets = oDoc.PropertySets

Dim oPropSet As PropertySet
Set oPropSet = oPropsets.Item("Inventor User Defined Properties")

Dim oCell As Range
' Loop through each parameter listed in sheet
For Each oCell In oSheet.Range(sParamRange)
    ' Parse through parameters and check to see if parameter name matches current parameter from Excel
    Dim oProp As Property
    For Each oProp In oPropSet
        ' If names match, copy value and units from Excel into parameter expression
        If oCell.Value = oProp.Name Then
            oProp.Expression = oCell.Offset(0, 1).Value
        End If
    Next oProp
Next oCell

Set oExcel = Nothing

' Update part/assembly
oDoc.Update
End Sub

 

 

0 Likes
Message 5 of 8

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @tonythm 

Instead of setting the Propertys expression, try setting its value.

For Each oCell In oSheet.Range(sParamRange)
    ' Parse through parameters and check to see if parameter name matches current parameter from Excel
    Dim oProp As Property
    For Each oProp In oPropSet
        ' If names match, copy value and units from Excel into parameter expression
        If oCell.Value = oProp.Name Then
            oProp.Value = oCell.Offset(0, 1).Value
        End If
    Next oProp
Next oCell
0 Likes
Message 6 of 8

tonythm
Advocate
Advocate
Accepted solution

Hi @JhoelForshav 

 

Thank you for helping me for this code.

My problem may have been solved with your code.
I have another small problem: when the value of the Number type is assigned, the value is 23.0 but I just want to be 23 (integers) not need .0 (decimals) in the back. Although in Excel I installed it is 23 but it still inputs into Inventor as 23.0
Can you help me with this small problem?
I really appreciate your help.

0 Likes
Message 7 of 8

JhoelForshav
Mentor
Mentor
Accepted solution

Hi @tonythm 

Glad I could help.

I'm thinking something like this might work for you?

For Each oCell In oSheet.Range(sParamRange)
    ' Parse through parameters and check to see if parameter name matches current parameter from Excel
    Dim oProp As Property
    For Each oProp In oPropSet
        ' If names match, copy value and units from Excel into parameter expression
        If oCell.Value = oProp.Name Then
        If IsNumeric(oCell.Offset(0, 1).Value) Then
            oProp.Value = CInt(oCell.Offset(0, 1).Value)
        Else
            oProp.Value = oCell.Offset(0, 1).Value
        End If
        End If
    Next oProp
Next oCell

 Check to see if the value is numeric, and if it is convert it to Integer before setting it to the property value.

0 Likes
Message 8 of 8

tonythm
Advocate
Advocate

Hello @JhoelForshav 

 

Thank you once again for helping me. You are wonderful.
Here is the complete code, so everyone can refer:

 

Sub GetProperties()

Dim oWkbk As Workbook
Set oWkbk = ThisWorkbook

Dim oSheet As Worksheet
Set oSheet = oWkbk.ActiveSheet

Dim sSheetName As String
sSheetName = "Sheet1"

Dim sParamRange As String
sParamRange = "A1:A6"

Dim oInvApp As Inventor.Application
Set oInvApp = GetObject(, "Inventor.Application")

Dim oDoc As Document
Set oDoc = oInvApp.ActiveDocument

Dim oPropsets As PropertySets
Set oPropsets = oDoc.PropertySets

Dim oPropSet As PropertySet
Set oPropSet = oPropsets.Item("Inventor User Defined Properties")

Dim oCell As range
' Loop through each parameter listed in sheet
For Each oCell In oSheet.range(sParamRange)
    ' Parse through parameters and check to see if parameter name matches current parameter from Excel
    Dim oProp As Property
    For Each oProp In oPropSet
        ' If names match, copy value and type from Excel into parameter expression
        If oCell.Value = oProp.Name Then
            If IsNumeric(oCell.Offset(0, 1).Value) Then
            oProp.Value = CInt(oCell.Offset(0, 1).Value)
        Else
            oProp.Value = oCell.Offset(0, 1).Value
            End If
        End If
    Next oProp
Next oCell
Set oExcel = Nothing
' Update part/assembly
oDoc.Update
End Sub
0 Likes