Dear All
I have written this code which runs through a series of Excel files in a directory, opens them one by one, change the language to English and saves and closes the file, this works fine.
However one of the files is corrupted and its giving me a RunTime Error 1004, ideally I want it to ignore the error not bother opening the file and continue onto opening the next file. I would also like it to log the issue in the ErrorLog.txt
Can anyone tell me where I am going wrong and how I can ignore the Runtime Error? Thanks
I have written this code which runs through a series of Excel files in a directory, opens them one by one, change the language to English and saves and closes the file, this works fine.
However one of the files is corrupted and its giving me a RunTime Error 1004, ideally I want it to ignore the error not bother opening the file and continue onto opening the next file. I would also like it to log the issue in the ErrorLog.txt
Can anyone tell me where I am going wrong and how I can ignore the Runtime Error? Thanks
Code:
Sub ChangeLanguage()
Application.ScreenUpdating = False
'Folderpath is where the XLCRM databases will eb saved and opened from
Dim folderPath As String
'Filename is the name of the XLCRM database that will be opened
Dim filename As String
'Workbook is the XLCRM database
Dim wb As Workbook
Dim writelog As String
Dim writelogfilename As String
folderPath = "V:\XLCRM\B Test\"
writelog = "V:\XLCRM\B Test\Test\Errors\"
writelogfilename = "ErrorLog.txt"
If Right(folderPath, 1) <> "\" Then folderPath = folderPath + "\"
filename = Dir(folderPath & "*CRM.xlsm")
Do While filename <> ""
Application.ScreenUpdating = False
Set wb = Workbooks.Open(folderPath & filename)
Application.Run "'" & filename & "'!English"
On Error Resume Next
If Not Err.Number = 0 Then
Open writelog & writelogfilename For Append As #1
Print #1, Now & " " & Err.Number & ":" & Err.Description
Close #1
End If
On Error GoTo 0
filename = Dir
SaveChanges = True
Application.EnableEvents = False
ActiveWindow.Close SaveChanges:=True
Application.DisplayAlerts = False
Loop
Application.ScreenUpdating = True
MsgBox "Language Change to English Complete"
End Sub