This code is opens a .XLSX file by use of the "xlExtractData" option, and then saves it with the same file name and path.
Application.DisplayAlerts = False
Set wb = Workbooks.Open(filename:=myPath & myFile, UpdateLinks:=0, CorruptLoad:=xlExtractData)
' Do not show the "This file already exists prompt"
Application.DisplayAlerts = False
' Save the repaired file
wb.SaveAs filename:=myPath & myFile, AccessMode:=xlExclusive, FileFormat:=xlOpenXMLWorkbook, CreateBackup:=False, ConflictResolution:=xlLocalSessionChanges
' Close the saved file
wb.Close
It is working for most of the files (there are hundreds of files) but it randomly does not work on some files. In those cases, it shows an error that says "Cannot access file".
Why would it work for some files and not work for others, even so randomly?