I am working on some code that will attach a user selected file onto an email. The code is in a class module, so I want to add some error handling in case the programming passes an invalid file. I have no problem if the error is #53 File Not Found. That's the one I am anticipating. But just in case some other error occurs for some other reason, I want it to throw the error. I tried using the Err.Raise, and I moved it just above the code where I would normally process the File not Found error. I did this just so I can see what it does when I throw it. But even though the original error was 53, because I intentionally sent it a bad file, it throws error 5 instead: Invalid Procedure call or argument.
Why is it throwing error number 5 and not error number 53, which is what the error really was?
-JTBorton
Another Day, Another Disaster
Code:
Set objFileSystem = CreateObject("Scripting.FileSystemObject")
On Error Resume Next
Set objTheFile = objFileSystem.getfile(FileName)
If Err.Number = 53 Then 'File not found
Err.Raise Number:=Err.Number, Source:=Err.Source, Description:=Err.Description, HelpFile:=Err.HelpFile, HelpContext:=Err.HelpContext
Err.Source = "Email Engine Add_Attachment"
MsgBox prompt:="Error number " & Err.Number & vbCrLf & "Source: " & Err.Source & vbCrLf & vbCrLf & Err.Description & vbCrLf & FileName, Buttons:=vbCritical, Title:="Email Engine Error"
Err.Clear
Set objFileSystem = Nothing
Set objTheFile = Nothing
Exit Sub
Else
Err.Raise Err.Number, Err.Source, Err.Description, Err.HelpFile, Err.HelpContext
End If
Why is it throwing error number 5 and not error number 53, which is what the error really was?
-JTBorton
Another Day, Another Disaster