Here is a VBA macro that you can use that will allow you manually enter the names of the components you want to replace, and the names of the files you want to replace them. I assumed that is how you wanted it set-up, since you didn't seem to like using 'Pick' to select the component to replace, and you wanted to be able to also provide part names to replace the components with each time. To use this VBA macro though, you would need to manually edit the code each time before running to change component names and replacement part names. If I'm wrong about your intent/meaning, the this code could fairly easily be modified to suit slightly different needs.
The public Sub "Replace" in the lower part, is set-up so that you can provide either the component's 'component name', or its 'file name without path' or its 'FullFileName', to make it easier, since it is set-up to 'replace all'. The "ReplacementName" or "newFileName" are set-up to use the 'FullFileName' of the replacement part/sub-assembly.
Other ideas would be to use two InputBoxes to ask the user for the component name, and the replacement part name ; or use the 'Pick' command to get the component, then use something like a FileDialog (or similar) to allow you to 'Browse' for the replacement file.
Here's the code:
Sub ReplaceAll()
If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
Call MsgBox("An Assembly Document must be active for this rule to work. Exiting.", vbOKOnly + vbCritical, "WRONG DOCUMENT TYPE")
Exit Sub
End If
Call Replace("ComponentName1", "ReplacementName1", True, True)
Call Replace("ComponentName2", "ReplacementName2", True, True)
Call Replace("ComponentName3", "ReplacementName3", True, True)
End Sub
Public Sub Replace(componentName As String, newFileName As String, replaceAll As Boolean, Optional SaveEdits As Boolean = vbNull, Optional KeepAdaptivity As Boolean = vbNull)
Dim oADoc As AssemblyDocument
Set oADoc = ThisApplication.ActiveDocument
Dim oADef As AssemblyComponentDefinition
Set oADef = oADoc.ComponentDefinition
Dim oMyComp As ComponentOccurrence
Dim oComp As ComponentOccurrence
Dim oFSO As New FileSystemObject
If oFSO.FileExists(newFileName) = False Then
Call MsgBox("No file could be found matching the specified replacement part's path/name. Exiting.", , "")
Exit Sub
End If
On Error GoTo LoopComps
Set oMyComp = oADef.Occurrences.ItemByName(componentName)
If Err = 0 Then GoTo ReplacementCall
LoopComps:
'that didn't work, so try looping through all components & check both 'file name' and 'FullFileName' of each
On Error GoTo 0
Dim oFName As String 'to hold file name, without path
For Each oComp In oADef.Occurrences
oFName = oFSO.GetFileName(oComp.Definition.Document.FullFileName)
If oFName = componentName Then
oMyComp = oComp
Exit For
End If
If oComp.Definition.Document.FullFileName = componentName Then
oMyComp = oComp
Exit For
End If
Next
If oMyComp Is Nothing Then
Call MsgBox("A component with the name provided could not be found. Exiting.", , "")
Exit Sub
End If
ReplacementCall:
Call oMyComp.Replace2(newFileName, replaceAll, SaveEdits, KeepAdaptivity)
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 have time, please... Vote For My IDEAS 💡or you can Explore My CONTRIBUTIONS
Wesley Crihfield

(Not an Autodesk Employee)