Are you sure the name of that custom iProperty "Article No" is spelled the same in all components and in the code?
Yes, I'm sure those Boolean type variables are False by default when they are first created. And they are created within the loop of components, so they are created new for each loop and can't maintain their value from the previous loop. But, if it will ease your mind, by all means go ahead and create them individually and set their values to False each time. What I did was just condensed into one line.
I have altered the code a bit to give the Boolean variables a definite value one way or the other when checking iProperty values. I also switched from the iProperty.Value() snippets to the normal Document.PropertySets.PropertySet.Property.Value route of accessing those iProperties, and incorporated an extra Try...Catch block of code around the part where I try to get the value of that custom iProperty, in case it doesn't exist yet, to avoid potential errors, and to know for sure whether it exists or not. There are multiple options for what we could put in the Catch portion of that check, but I just put a message in there for now. I also created more lines for other possibilities within the If...ElseIf...Then block of code where the component name is assembled & assigned, just in case some components may not have their Part Number and/or Title filled out either. (There are 6 possible combinations of those 3 iProperties having values.)
Here's the updated code:
Sub Main
Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim oDef As AssemblyComponentDefinition = oDoc.ComponentDefinition
SetOccName(oDef)
End Sub
Sub SetOccName(oADef As AssemblyComponentDefinition)
Dim i As Integer = 0
For Each oOcc As ComponentOccurrence In oADef.Occurrences
Dim oOccDoc As Document = oOcc.Definition.Document
Dim oPSets As PropertySets = oOccDoc.PropertySets
Dim oPNEmpty, oArticleNoEmpty, oTitleEmpty As Boolean 'False by default
Dim oPN As String = oPSets.Item(3).Item("Part Number").Value
If String.IsNullOrEmpty(oPN) Then oPNEmpty = True Else oPNEmpty = False
Dim oTitle As String = oPSets.Item(1).Item("Title").Value
If String.IsNullOrEmpty(oTitle) Then oTitleEmpty = True Else oTitleEmpty = False
Dim oArticleNo As String
Try
'the custom iProperty may not exist, so use Try...Catch block to check it
oArticleNo = oPSets.Item(4).Item("Article No").Value
oArticleNoEmpty = False
Catch
'oArticleNoEmpty = True
'above failed, so it was not found
'you could try to create it here, or just ignore it, or use a message to let you know
MsgBox("Custom iProperty 'Article No' was not found it component named " & oOcc.Name,,"")
End Try
If String.IsNullOrEmpty(oArticleNo) Then oArticleNoEmpty = True Else oArticleNoEmpty = False
Try
If oPNEmpty = False And oArticleNoEmpty = False And oTitleEmpty = False Then 'all present
oOcc.Name = oPN & " (" & oArticleNo & " " & oTitle & ")" & "-" & i
ElseIf oPNEmpty = False And oArticleNoEmpty = True And oTitleEmpty = False Then 'ArticleNo missing
oOcc.Name = oPN & " (" & oTitle & ")" & "-" & i
ElseIf oPNEmpty = True And oArticleNoEmpty = False And oTitleEmpty = False Then 'PN missing
oOcc.Name = "(" & oArticleNo & " " & oTitle & ")" & "-" & i
ElseIf oPNEmpty = False And oArticleNoEmpty = False And oTitleEmpty = True Then 'Title missing
oOcc.Name = oPN & "(" & oArticleNo & ")" & "-" & i
End If
Catch
MsgBox("Assigning new name to component named '" & oOcc.Name & "' failed.", , "")
End Try
i = i + 1
If TypeOf oOcc.Definition Is AssemblyComponentDefinition Then
SetOccName(oOcc.Definition)
End If
Next
End Sub
If this solved your problem, or answered your question, please click ACCEPT SOLUTION.
Or, if this helped you, please click (LIKE or KUDOS) 👍.
If you want and have time, I would appreciate your Vote(s) for My IDEAS 💡or you can Explore My CONTRIBUTIONS
Wesley Crihfield

(Not an Autodesk Employee)