Hi @Anonymous. I'm not sure why you are looping through all of the main assembly's referenced documents, if you are simply going to call out a few very specific components by their names to check and or replace. That part doesn't make sense to me, so when I created an alternative iLogic code, I left that whole process out. The document you are checking to make sure it is a PartDocument then checking to make sure it is a Content Center member, is not necessarily guaranteed to be the same document as any of the named components you are specifying within the code after that, so I was not sure which of the named components you may have needed to check these aspects of before proceeding. My code is laid out quite differently than yours, and includes a lot of code that is attempting to avoid potential errors. I am also avoiding use of those Component.IsActive() and Component.Replace() calls (iLogic snippets) and trying to use purely API type calls, to help avoid any confusion.
Here is the new code I created. I obviously have not tested it yet though, because I do not have your data set to test on.
Sub Main
Dim oADoc As AssemblyDocument = ThisApplication.ActiveDocument
oADef = oADoc.ComponentDefinition
oOccs = oADef.Occurrences
'get the 4 named components (from any level of the assembly)
oOcc1 = GetNamedComponent(oOccs, "ISO 4017 M12x20_A4-80:4")
If IsNothing(oOcc1) Then
MsgBox("The component named '" & oOcc1.Name & "' could not be found. Exiting rule.", vbCritical, "iLogic")
Exit Sub
End If
oOcc2 = GetNamedComponent(oOccs, "ISO 4017 M12x20_A4-80:1")
If IsNothing(oOcc2) Then
MsgBox("The component named '" & oOcc2.Name & "' could not be found. Exiting rule.", vbCritical, "iLogic")
Exit Sub
End If
oOcc3 = GetNamedComponent(oOccs, "ISO 4017 M8x20_A4-80:1")
If IsNothing(oOcc3) Then
MsgBox("The component named '" & oOcc3.Name & "' could not be found. Exiting rule.", vbCritical, "iLogic")
Exit Sub
End If
oOcc4 = GetNamedComponent(oOccs, "ISO 4017 M8x20_A4-80:3")
If IsNothing(oOcc4) Then
MsgBox("The component named '" & oOcc4.Name & "' could not be found. Exiting rule.", vbCritical, "iLogic")
Exit Sub
End If
Dim asfile As String
If oOcc1.Suppressed = False Then 'not suppressed = IsActive
asfile = "C:\VaultWS\Library\en-US\ISO 4014(2)\B001916.ipt"
Try
oOcc2.Replace(asfile, True)
Catch oEx As Exception
MsgBox("The attempt to replace '" & oOcc2.Name & "' with '" & asfile & "' Failed." & vbCrLf & _
oEx.Message & vbCrLf & oEx.StackTrace, vbCritical, "iLogic")
End Try
ElseIf oOcc3.Suppressed = False Then
asfile = "C:\VaultWS\Library\en-US\ISO 4014(2)\B001895.ipt"
Try
oOcc4.Replace(asfile, True)
Catch oEx As Exception
MsgBox("The attempt to replace '" & oOcc4.Name & "' with '" & asfile & "' Failed." & vbCrLf & _
oEx.Message & vbCrLf & oEx.StackTrace, vbCritical, "iLogic")
End Try
End If
End Sub
Function GetNamedComponent(oComps As ComponentOccurrences, oCompnentName As String) As ComponentOccurrence
For Each oComp As ComponentOccurrence In oComps
If oComp.Name = oCompnentName Then
Return oComp
End If
If oComp.DefinitionDocumentType = DocumentTypeEnum.kAssemblyDocumentObject Then
oSComp = GetNamedComponent(oComp.Definition.Occurrences, oCompnentName)
If Not IsNothing(oSComp) Then Return oSComp
End If
Next
'if code reaches this point, the component was not found, so return Nothing
Return Nothing
End Function
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)