Option Explicit
Sub BreakLinksInFiles()
Dim sourceFolder As String
Dim destFolder As String
Dim pathAndFilename As String
Dim targetFiles As Variant
Dim currentFile As Variant
Dim currentWorkbook As Workbook
Dim linkSources As Variant
Dim currentLink As Variant
Dim fileCount As Long
'set the path to the source folder (change accordingly)
sourceFolder = "C:\Users\Domenic\Desktop\Folder1\"
'ensure path to the source folder ends with a backslash (\)
If Right(sourceFolder, 1) <> "\" Then
sourceFolder = sourceFolder & "\"
End If
'make sure source folder exists
If Len(Dir(sourceFolder, vbDirectory)) = 0 Then
MsgBox "'" & sourceFolder & "' not found!", vbExclamation
Exit Sub
End If
'set the path to the destination folder (change accordingly)
destFolder = "C:\Users\Domenic\Desktop\Folder2\"
'ensure the path to the destination folder ends with a backslash (\)
If Right(destFolder, 1) <> "\" Then
destFolder = destFolder & "\"
End If
'make sure destination folder exists
If Len(Dir(destFolder, vbDirectory)) = 0 Then
MsgBox "'" & destFolder & "' not found!", vbExclamation
Exit Sub
End If
With Application
.DisplayAlerts = False
.ScreenUpdating = False
End With
'set the list of files to process (add and/or change the names accordingly)
targetFiles = Array("file1.xlsm", "file2.xlsm", "file3.xlsm")
'loop through each target file for processing
fileCount = 0
For Each currentFile In targetFiles
'set the path and filename for the current file
pathAndFilename = sourceFolder & currentFile
'process file, if it exists
If Len(Dir(pathAndFilename, vbNormal)) > 0 Then
'increment file count
fileCount = fileCount + 1
'open the current file and update the links
Set currentWorkbook = Workbooks.Open(Filename:=pathAndFilename, UpdateLinks:=True)
'get the links from the current workbook
linkSources = currentWorkbook.linkSources(xlExcelLinks)
'break the links in the current workbook, if they exist
If Not IsEmpty(linkSources) Then
For Each currentLink In linkSources
currentWorkbook.BreakLink currentLink, xlLinkTypeExcelLinks
Next currentLink
End If
With currentWorkbook
'save the current workbook to the destination folder, overwrite if one with the same name already exists
.SaveAs Filename:=destFolder & currentWorkbook.Name
'close the current workbook
.Close savechanges:=False
End With
End If
Next currentFile
'clear from memory
Set currentWorkbook = Nothing
With Application
.DisplayAlerts = True
.ScreenUpdating = True
End With
MsgBox "Processed files: " & fileCount, vbInformation
End Sub