I have a loop that opens word documents however there are a few that are corrupted, I can't open them manually either,
The code I'm using to open is,
Set newdoc = objWord.Documents.Open(saveFolder & Filename)
This fails with the error
"Run-time error 5792, the file appears to be corrupted".
Is it possible to tell that the file is corrupt before trying to open it?
CodePudding user response:
No, but you can test it while trying to open it.
On Error Resume Next ' deactivate error reporting
Set newdoc = Nothing
Set newdoc = objWord.Documents.Open(saveFolder & Filename)
' check if trying to open it produced an error
If Err.Number = 5792 Then
Msgbox "'" & saveFolder & Filename & "' is corrupt."
Err.Clear
End If
On Error Goto 0 ' re-activate error reporting. Do not forget this!
' if an error occured during opening then newdoc is nothing
If Not newdoc Is Nothing
' do what you want to do with the opened file
Else
' do what you want to do with a corrupt file
End If
CodePudding user response:
your best bet here is to use error handling.
Something like
On Error GoTo skip
Set newdoc = objWord.Documents.Open(saveFolder & Filename)
On Error GoTo 0
Exit Function
skip: