Announcements
Attention for Customers without Multi-Factor Authentication or Single Sign-On - OTP Verification rolls out April 2025. Read all about it here.
MTheDesigner
356 Views, 3 Replies

Relink references to renamed files

I wrote some code that can go through all my referenced documents, change the job number of the item, save it, and then delete the old file. 

 

However this causes all the links in my assemblies to break. I know there is a FileDecriptor.ReplaceRefrence command, but I do not know how to make sure that the FileDesctriptor is the same document as the on in AllReferencedDocuments.

 

Because I am using a for each loop, I don't know how to address the FileDescripters enum.

 

Here Is the code I have so far

If ThisApplication.ActiveDocumentType <> DocumentTypeEnum.kAssemblyDocumentObject Then
	MsgBox("please only use on an assembly")
	Exit Sub
End If
Dim oDoc As AssemblyDocument = ThisApplication.ActiveDocument
Dim sOldProjectNumber As String = InputBox("Enter old project number", "Old Number",)
Dim sNewProjectNumber As String = InputBox("Enter new project number", "New Number")
Dim oRefDoc As Document
For Each oRefDoc In oDoc.AllReferencedDocuments
	Dim sOldName As String = System.IO.Path.GetFileName(oRefDoc.FullFileName)
	If sOldName.Contains(sOldProjectNumber) Then
		Dim sPath As String = System.IO.Path.GetDirectoryName(oRefDoc.FullFileName)
		Dim sNewName As String = sOldName.Replace(sOldProjectNumber, sNewProjectNumber)
		Try
			Dim oRefPropSet As PropertySet = oRefDoc.PropertySets.Item("Design Tracking Properties")
			Dim oRefProp As [Property] = oRefPropSet.Item("Part Number")
			oRefProp.Value = sNewName
		Catch
			MsgBox(System.IO.Path.GetFileName(oRefDoc.FullFileName))
		End Try
		oRefDoc.SaveAs(sPath & "\" & sNewName, True)
		Dim oDocDesc As FileDescriptor = oDoc.ReferencedDocumentDescriptors.Item(oRefDoc)
		oDocDesc.
		System.IO.File.Delete(sPath & "\" & sOldName)
	End If
Next