How to update custom iProperties with VBA?

How to update custom iProperties with VBA?

JDidrik
Contributor Contributor
6,474 Views
5 Replies
Message 1 of 6

How to update custom iProperties with VBA?

JDidrik
Contributor
Contributor
Hi,
I am trying to write an simple VBA code which gives me the opportunity to automatically export the dimensions of the part to custom iProperties, but when the iProperty has been created the first time, I am not able do update/overwrite the property with updated values. Could someone explain to me what I need to do?

Jan Didrik

0 Likes
Accepted solutions (1)
6,475 Views
5 Replies
Replies (5)
Message 2 of 6

MechMachineMan
Advisor
Advisor

vb.net is easier to use for accessing properties due to the try catch statement:

 


Try oProp = oDoc.PropertySets.Item("Inventor User Defined Properties").Item("DESCRIP" & m)
Catch oProp = oDoc.PropertySets.Item("Inventor User Defined Properties").Add("", "DESCRIP" & m) End Try


To do the same thing in vba, you have to scratch the try catch clause and add in either error handling, or a check to ensure the property exists/doesn't.


--------------------------------------
Did you find this reply helpful ? If so please use the 'Accept as Solution' or 'Like' button below.

Justin K
Inventor 2018.2.3, Build 227 | Excel 2013+ VBA
ERP/CAD Communication | Custom Scripting
Machine Design | Process Optimization


iLogic/Inventor API: Autodesk Online Help | API Shortcut In Google Chrome | iLogic API Documentation
Vb.Net/VBA Programming: MSDN | Stackoverflow | Excel Object Model
Inventor API/VBA/Vb.Net Learning Resources: Forum Thread

Sample Solutions:Debugging in iLogic ( and Batch PDF Export Sample ) | API HasSaveCopyAs Issues |
BOM Export & Column Reorder | Reorient Skewed Part | Add Internal Profile Dogbones |
Run iLogic From VBA | Batch File Renaming| Continuous Pick/Rename Objects

Local Help: %PUBLIC%\Documents\Autodesk\Inventor 2018\Local Help

Ideas: Dockable/Customizable Property Browser | Section Line API/Thread Feature in Assembly/PartsList API Static Cells | Fourth BOM Type
Message 3 of 6

rossano_praderi
Collaborator
Collaborator

Hi Jan,

you mean something like this?

 

Sub ChangeIprop()
    Dim oDoc As Document
    On Error Resume Next
    Set oDoc = ThisApplication.ActiveDocument
    oProp = oDoc.PropertySets.Item("Inventor User Defined Properties").Add("", "DESCRIPTION")
    oDoc.PropertySets.Item("Inventor User Defined Properties").Item("DESCRIPTION").Value = "test"
End Sub

Bregs

Rossano Praderi



--------------------------------------
If my post answers your question, please click the "Accept as Solution"
button. This helps everyone find answers more quickly!
---------------
0 Likes
Message 4 of 6

JDidrik
Contributor
Contributor

Rossano,

 

I believe thats exactly what I am looking for, but I can not get it to work. See my code below. If the xRange iProperty does not exist, then I am amble to add it. But if it allready exists, I am not able to upadate it.

 

 

 

Public Sub UpdateRangeParameters()
' Get the active part document.
Dim invPartDoc As PartDocument
Set invPartDoc = ThisApplication.ActiveDocument

Dim totalRange As Box
Set totalRange = invPartDoc.ComponentDefinition.RangeBox

Dim xSize As String
Dim ySize As String
Dim zSize As String

xSize = invPartDoc.UnitsOfMeasure.GetStringFromValue(totalRange.MaxPoint.X - totalRange.MinPoint.X, kDefaultDisplayLengthUnits)
ySize = invPartDoc.UnitsOfMeasure.GetStringFromValue(totalRange.MaxPoint.Y - totalRange.MinPoint.Y, kDefaultDisplayLengthUnits)
zSize = invPartDoc.UnitsOfMeasure.GetStringFromValue(totalRange.MaxPoint.Z - totalRange.MinPoint.Z, kDefaultDisplayLengthUnits)

' Get the custom property set.
Dim customPropSet As PropertySet
Set customPropSet = invPartDoc.PropertySets.Item("Inventor User Defined Properties")


'X-RANGE
' Attempt to get an existing custom property named "xRange".
On Error Resume Next
Dim xRange As Property
Set xRange = customPropSet.Add(xSize, "xRange")
If Err.Number <> 0 Then
' Property does not exist -> Add the property. Call customPropSet.Add(xSize, "xRange") Else 'Property exist -> update the value. invPartDoc.PropertySets.Item("Inventor User Defined Properties").Item("xRange").Value = "test" End If MsgBox ("Custom iProperties updated") End Sub

 

Jan Didrik

0 Likes
Message 5 of 6

Owner2229
Advisor
Advisor
Accepted solution

Hi, I have no idea how it could work for creation of the new iProperty, but you had a misstake in your code.

As I believe, there should be "User Defined Properties" instead of "Inventor User Defined Properties".

Atleast it works like that for me.

See the corrected version:

 

Public Sub UpdateRangeParameters()
' Get the active part document.
Dim invPartDoc As PartDocument
Set invPartDoc = ThisApplication.ActiveDocument

Dim totalRange As Box
Set totalRange = invPartDoc.ComponentDefinition.RangeBox

Dim xSize As String
Dim ySize As String
Dim zSize As String

xSize = invPartDoc.UnitsOfMeasure.GetStringFromValue(totalRange.MaxPoint.X - totalRange.MinPoint.X, kDefaultDisplayLengthUnits)
ySize = invPartDoc.UnitsOfMeasure.GetStringFromValue(totalRange.MaxPoint.Y - totalRange.MinPoint.Y, kDefaultDisplayLengthUnits)
zSize = invPartDoc.UnitsOfMeasure.GetStringFromValue(totalRange.MaxPoint.Z - totalRange.MinPoint.Z, kDefaultDisplayLengthUnits)

' Get the custom property set.
Dim customPropSet As PropertySet
Set customPropSet = invPartDoc.PropertySets.Item("User Defined Properties")

'X-RANGE
' Attempt to get an existing custom property named "xRange".
On Error Resume Next
Dim xRange As Property
Set xRange = customPropSet.Item("xRange")
If Err.Number <> 0 Then
' Property does not exist -> Add the property. Call customPropSet.Add(xSize, "xRange") Else 'Property exist -> update the value. xRange.Value = "test" End If MsgBox ("Custom iProperties updated") End Sub

 

Consider using "Accept as Solution" / "Kudos" if you find this helpful.
- - - - - - - - - - - - - - -
Regards,
Mike

"Always code as if the guy who ends up maintaining your code will be a violent psychopath who knows where you live." - John F. Woods
0 Likes
Message 6 of 6

JDidrik
Contributor
Contributor

Thanks Mike!

 

You found the problem. The problem was the "set xRange = customPropSet.Add."

 

When I changed the code from:

'X-RANGE
' Attempt to get an existing custom property named "xRange".
On Error Resume Next
Dim xRange As Property
Set xRange = customPropSet.Add(xSize, "xRange")

 

 

to your correction:

 

'X-RANGE
' Attempt to get an existing custom property named "xRange".
On Error Resume Next
Dim xRange As Property
Set xRange = customPropSet.Item("xRange")

 

it now works as intended.

Thanks a lot!

 

Jan Didrik

0 Likes