im looking for an item "customer code" in the custom prop of a part
normally, i have 10+ parts in an assy.
can vba access the udp of a part from assy?
and show which part doesnt have that item?
or vise versa or both?
Solved! Go to Solution.
Solved by ekinsb. Go to Solution.
What you want to do is certainly possible using the API but to be able to give you as good of a sample as possible some more information would be useful.
When looking for this particular property in parts, do you want to look at all levels of the assembly or only the top-level. Do you want to look for this only in parts or also sub assemblies?
Here's one way to do it.
Public Sub GetiPropertiesOfTopLevel() ' Get the active assembly. Dim asmDoc As AssemblyDocument Set asmDoc = ThisApplication.ActiveDocument ' Iterate through the files referenced by the assembly. ' These are only direct references so parts and assemblies ' in subassemblies will be ignored. Dim doc As Document For Each doc In asmDoc.ReferencedFiles ' Get the "Customer Code" iProperty Dim customPropSet As PropertySet Set customPropSet = doc.PropertySets.item( _ "Inventor User Defined Properties") Dim prop As Inventor.Property On Error Resume Next Set prop = customPropSet.item("Customer Code") If Err.Number = 0 Then Debug.Print "The file """ & doc.FullFileName & """ has the value: " & prop.Value Else Debug.Print "The file """ & doc.FullFileName & _ """ does not have the ""Customer Code"" iProperty" End If On Error GoTo 0 Next End Sub
Here's a modified version of the previous program that writes the results to a file. I'm formatting with commas and using a .csv extension so it will open in Excel but you can easily modify it to output it any way you want.
Public Sub GetiPropertiesOfTopLevel() ' Get the active assembly. Dim asmDoc As AssemblyDocument Set asmDoc = ThisApplication.ActiveDocument ' Open the file to write the results. Dim filename As String filename = "C:\Temp\PropertyResults.csv" Open filename For Output As #1 Print #1, "Filename,Has Customer Code,Value" ' Iterate through the files referenced by the assembly. ' These are only direct references so parts and assemblies ' in subassemblies will be ignored. Dim doc As Document For Each doc In asmDoc.ReferencedFiles ' Get the "Customer Code" iProperty Dim customPropSet As PropertySet Set customPropSet = doc.PropertySets.item( _ "Inventor User Defined Properties") Dim prop As Inventor.Property On Error Resume Next Set prop = customPropSet.item("Customer Code") If Err.Number = 0 Then Print #1, doc.FullFileName & ",True," & prop.Value Else Print #1, doc.FullFileName & ",False," End If On Error GoTo 0 Next Close #1 MsgBox "Finished processing." & vbCrLf & _ "Results written to """ & filename & """" End Sub
this is so perfect !!!
thank you so much...
ive change the message to give user option to choose to open the csv file
but i get error when it reaches to the point where it about to open the file.
i think my code for opening a csv is wrong.
can you help me pls
ive been googling and cant find some example...