Mar
11
renaming linked spreadsheets and reports
Written by
rose
Is there a way to change the link automatically when linked documents are both saved with different names?
Thanks
Thanks again.
I then paste the following code:
Private y As Boolean
Private Sub Workbook_BeforeClose(Cancel As Boolean)
y = True
End Sub
Private Sub Workbook_BeforeSave(ByVal myVar As Boolean, Cancel As Boolean)
If Not (y) Then Application.OnTime Now, "ThisWorkbook.AfterSave"
End Sub
Private Sub AfterSave()
Dim MyDir As String, fso As Object, Nme As String, y As Long, oldLinks
Set fso = CreateObject("Scripting.FileSystemObject")
MyDir = "c:temp" 'Change Directory
Nme = "Report " & Replace(ThisWorkbook.Name, "Workpaper", "") '"Report #"
If fso.FileExists(MyDir & Nme) Then
oldLinks = ActiveWorkbook.LinkSources(xlExcelLinks)
For y = LBound(oldLinks) To UBound(oldLinks)
ThisWorkbook.ChangeLink oldLinks(y), MyDir & Nme
Next
End If
End Sub
The code does a few things. First, it waits for the save to grab the new name, not the old one. Second, it looks for Report X on the c:temp drive, if it finds it, it proceeds to update the links, otherwise it aborts.
Questions or concerns, please let us know. Hope this helps.
Edit: Added code so that if you save on close, Excel won't fire the application level ontime procedure, which will attempt to reopen the workbook.
#If you have any other info about this subject , Please add it free.# |