I have read through several posts of renaming and changing references, and they are all almost there.
I am looking to get the path from the old file reference and using it as base reference for the renaming work.
C:\abc\123\qwe\FileOld.dng (the one attatched)
C:\abc\123\qwe\FileNew.dng
Question:
The question here is how do I get the path of a reference in this case FileOld.dng into a String variable by just giving the input FileOld.dng
The idea is to just write change FileOld.dng to FileNew.dng and the VBA looks at the Path of the old one.
Example: Key in - vba run [refReplace]refWorks FileOld.dng FileNew.dng
Step one get: C:\abc\123\qwe\
Step 2 rename and use this path to make a working reference change.
Good examples from the forum that almost made it for me.
A: In this one you need to manually add the path
RefRename.mvba
OldRefName = "D:\1000\ABC101\Ref\Border-old.dgn"
NewRefName = "D:\1000\ABC101\Ref\Border.dgn"
B: This one says that it changes the reference, and it does! But the reference gets broken even if they are in the same folder,
VBA change references
OldString = "ExistingTitleblock.DGN"
newString = "NewTitleblock.DGN"
C: This one can rename a path to a file and it has a fantastic dialogue that pops up when running without parameters.
Rename full path data of reference
Working VBA ref_drive.zip
I think there is potential in this statment.
oAttachment.DesignFile.FullName
oAttachment.DesignFile.Fullpath (or something similar, this was just a guess from my side)
So in Leonard Jones example the code could be updated from the on below with the heding OLD to the one below with the heading NEW
OLD
Sub ReplaceRefAttachment()
Dim oAttachment As Attachment
Dim oAttachments As Attachments
Dim RefName As String
Dim OldRefName As String
Dim NewRefName As String
Set oAttachments = ActiveModelReference.Attachments
OldRefName = "D:\1000\ABC101\Ref\Border-old.dgn"
NewRefName = "D:\1000\ABC101\Ref\Border.dgn"
For Each oAttachment In oAttachments
RefName = oAttachment.DesignFile.FullName
If RefName = OldRefName Then
Debug.Print "Reference File Found..."
Set Attachment = oAttachment.Reattach(NewRefName, vbNullString)
End If
Next
End Sub
NEW added code in yellow
Note this is me trying to explain how I want it to work, and not a verified code
Sub ReplaceRefAttachment() Dim oAttachment As Attachment Dim oAttachments As Attachments Dim RefName As String Dim GetPath As String Dim OldRefName As String Dim NewRefName As String Set oAttachments = ActiveModelReference.Attachments OldRefName = "Border-old.dgn" NewRefName = "Border.dgn" For Each oAttachment In oAttachments RefName = oAttachment.DesignFile.FullName GetPath = Replace(RefName, OldRefName, "", 1,-1) 'I took this code to get the path without the name RefName = Replace(RefName, GetPath , "", 1,-1) 'I took this code to get the name without the path If RefName = OldRefName Then Debug.Print "Reference File Found..." Set Attachment = oAttachment.Reattach(GetPath & NewRefName, vbNullString) 'I added a GetPath & to concatenate the path with the new name End If Next End Sub
Sub ReplaceRefAttachment()
Dim oAttachment As Attachment
Dim oAttachments As Attachments
Dim RefName As String
Dim GetPath As String
Dim OldRefName As String
Dim NewRefName As String
Set oAttachments = ActiveModelReference.Attachments
OldRefName = "Border-old.dgn"
NewRefName = "Border.dgn"
For Each oAttachment In oAttachments
RefName = oAttachment.DesignFile.FullName
GetPath = Replace(RefName, OldRefName, "", 1, -1) 'I took this code to get the path without the name
RefName = Replace(RefName, GetPath , "", 1, -1) 'I took this code to get the name without the path
If RefName = OldRefName Then
Debug.Print "Reference File Found..."
Set Attachment = oAttachment.Reattach(GetPath & NewRefName, vbNullString)'I added a GetPath & to concatenate the path with the new name
End If
Next
End Sub
I hope this is interesting enough for someone to dig in to. I think it will benefit the community :)