Give this a convert from VB.Net and then check it out.
''' <summary>
''' Add Custom iProperty To Inventor Document
''' </summary>
''' <param name="propName"></param>
''' <param name="propValue"></param>
''' <param name="invDoc"></param>
''' <param name="OverWriteProperty"></param>
''' <returns>True if file was changed</returns>
''' <remarks>Includes all file states, readonly and unsaved new files</remarks>
Public Function CreateAndEditCustomProperty(ByVal propName As String, ByVal propValue As Object, ByVal invDoc As Inventor.Document, Optional OverWriteProperty As Boolean = True) As Boolean
Try
eventLog += vbCrLf & invDoc.FullFileName
Dim propSet As PropertySet = invDoc.PropertySets.Item("Inventor User Defined Properties")
Dim custProp As Inventor.Property = FindiProperty(propSet.Name, propName, invDoc)
If custProp Is Nothing Then
custProp = propSet.Add(propValue, propName)
filesEffected += 1
eventLog += vbCrLf & "Added custom property: " & propName & ": " & propValue
Return True
Else
If Not String.IsNullOrWhiteSpace(custProp.Value) Then ' has a value first check if expression, then if regular property
If OverWriteProperty = True Then
If custProp.Expression.StartsWith("=") Then 'is expression
If custProp.Expression <> propValue Then
custProp.Value = propValue
filesEffected += 1
eventLog += vbCrLf & "Modified custom property: " & propName & ": " & propValue
Return True
End If
Else 'is value
If custProp.Value <> propValue Then
custProp.Value = propValue
filesEffected += 1
eventLog += vbCrLf & "Modified custom property: " & propName & ": " & propValue
Return True
End If
End If
End If
Else 'is emtpy - overwrite command does snot apply
custProp.Value = propValue
filesEffected += 1
eventLog += vbCrLf & "Modified custom property: " & propName & ": " & propValue
Return True
End If
End If
Return False
Catch ex As Exception
'if property fails to write such as user does not check out or allow file to save, then retun without error.
MsgBox("Can not write custom property: " & propName)
Return False
End Try
End Function
btw eventLog and filesEffected are a public shared string and integer values I use when running this program as part of a much larger system. It reports possible errors for the user to read about. You can omit that feature, or change it up as you see fit.
Jamie Johnson : Owner / Sisu Lissom, LLC https://sisulissom.com/