I have some code that does this very thing. You will have to modify it to get the new names and paths. But what it does is look through all the document references to for references that are unresolved. Looks up the file name in a spreadsheet that contains the new number, and reformats the file name to the new path. It then uses the ResolveFile method to get the location of the new file. If it finds it it replaces the reference with the new location. If it can't replace the reference (as the files may not be a renamed copy of the file but something different) it replaces the occurrence of the file in the assembly).
Imports SysIO = System.IO
Public Sub Main()
Dim lookupSpreadSheet = "PathToSpreadsheet.xlsx"
Dim sheetName = "Parts"
Dim column = "PartNumber"
Dim docDescriptors = New List(Of DocumentDescriptor)
For Each docDesc As DocumentDescriptor In ThisDoc.Document.ReferencedDocumentDescriptors
If docDesc.ReferenceMissing AndAlso Not docDesc.ReferenceSuppressed Then
docDescriptors.Add(docDesc)
End If
Next
For Each doc As Document In ThisDoc.Document.AllReferencedDocuments
docDescriptors.AddRange(GetMissingReferences(doc))
Next
If docDescriptors.Any() Then
'MessageBox.Show(String.Join(vbCrLf, docDescriptors.Select(
'Function(d As DocumentDescriptor)
' Return GetLookupNumber(d)
'End Function )))
Dim newNumbers = New List(Of DescPair)
For Each desc As DocumentDescriptor In docDescriptors
Dim nameParts As Parts = GetLookupNumber(desc)
Dim row = GoExcel.FindRow(lookupSpreadSheet, sheetName, column, "=", nameParts.LookupValue)
Dim newNumber As String = ""
If row > 0 Then
nameParts.LookupResult = GoExcel.CellValue("A" & row).ToString()
Else
nameParts.LookupResult = SysIO.Path.GetFileNameWithoutExtension(desc.FullDocumentName)
End If
newNumbers.Add(New DescPair() With {.NameParts = nameParts, .Descriptor = desc})
Next
newNumbers.ForEach(Sub(d As DescPair) ReplaceReference(d))
End If
ThisDoc.Document.Update2(True)
End Sub
Public Function GetMissingReferences(doc As Document) As List(Of DocumentDescriptor)
Dim docDescriptors = New List(Of DocumentDescriptor)
For Each docDesc As DocumentDescriptor In doc.ReferencedDocumentDescriptors
If docDesc.ReferenceMissing AndAlso Not docDesc.ReferenceSuppressed Then
docDescriptors.Add(docDesc)
End If
Next
Return docDescriptors
End Function
Public Function GetLookupNumber(docDesc As DocumentDescriptor) As Parts
Dim rParts As Parts = New Parts()
Dim lookupValue = SysIO.Path.GetFileNameWithoutExtension(docDesc.FullDocumentName)
Dim nameParts = lookupValue.Split(" ")
If nameParts.Length > 1 Then
lookupValue = nameParts(0)
rParts.Rest = String.Join(" ", nameParts.Skip(1))
End If
Dim parts = lookupValue.Split("-")
If parts.Length > 1 Then
If parts(0).Length = 2 Then
lookupValue = "0" & lookupValue
End If
End If
rParts.LookupValue = lookupValue
Return rParts
End Function
Public Sub ReplaceReference(dp As DescPair)
Dim docDesc As DocumentDescriptor = dp.Descriptor
Dim newNumber As String = dp.NameParts.LookupResult
If Not String.IsNullOrWhiteSpace(dp.NameParts.Rest)
newNumber = newNumber & " " & dp.NameParts.Rest
End If
Dim newName = newNumber & SysIO.Path.GetExtension(docDesc.FullDocumentName)
Dim oldName = SysIO.Path.GetFileName(docDesc.FullDocumentName)
Dim newPath = ThisApplication.DesignProjectManager.ResolveFile(SysIO.Path.GetDirectoryName(ThisDoc.Document.FullDocumentName), newName, Nothing)
If Not String.IsNullOrEmpty(newPath) Then
Dim repDoc As Document = ThisApplication.Documents.Open(newPath, False)
Try
docDesc.ReferencedFileDescriptor.ReplaceReference(newPath)
Catch
ReplaceByOccurrence(dp, newPath)
End Try
repDoc.Close()
Else
Dim oldPath = ThisApplication.DesignProjectManager.ResolveFile(SysIO.Path.GetDirectoryName(ThisDoc.Document.FullDocumentName), oldName, Nothing)
If Not String.IsNullOrEmpty(oldPath) Then
Dim repDoc As Document = ThisApplication.Documents.Open(oldPath, False)
Try
docDesc.ReferencedFileDescriptor.ReplaceReference(oldPath)
Catch
ReplaceByOccurrence(dp, oldPath)
End Try
repDoc.Close()
End If
End If
End Sub
Public Sub ReplaceByOccurrence(dp As DescPair, newPath As String)
Dim parent As Document = dp.Descriptor.Parent
Dim asmDoc As AssemblyDocument = TryCast(parent, AssemblyDocument)
If asmDoc Is Nothing Then Return
Dim asmDef As AssemblyComponentDefinition = asmDoc.ComponentDefinition
For Each occ As ComponentOccurrence In asmDef.Occurrences
If occ.ReferencedDocumentDescriptor.FullDocumentName = dp.Descriptor.FullDocumentName Then
occ.Replace(newPath, True)
Exit For
End If
Next
End Sub
Public Structure DescPair
Public Descriptor As DocumentDescriptor
Public NameParts As Parts
End Structure
Public Structure Parts
Public LookupValue As String
Public LookupResult As String
Public Rest As String
End Structure