Error Trapping

jstevens
GoldLounger
Posts: 2628
Joined: 26 Jan 2010, 16:31
Location: Southern California

Error Trapping

Post by jstevens »

I am having a challenge trapping error 53-File Not found.

Here is a sample of code I am using:

Code: Select all

'      On Error GoTo Skip 'in the event of a permissions error; remarked out to test the message box below 
      
             If Err.Number <> 0 Then
                MsgBox Err.Number
                    GoTo Skip
             End If

       Set objFile = objFSO.GetFile(.FoundFiles(x)) 'set the object to get it's properties; HERE is where it errors out.
Once the code errors out, I am able to step through the code by pushing back the start point to "If Err.Number <> 0 Then" and things work just fine.

Any suggestions would be appreciated.

Thanks,
John
Regards,
John

User avatar
HansV
Administrator
Posts: 78370
Joined: 16 Jan 2010, 00:14
Status: Microsoft MVP
Location: Wageningen, The Netherlands

Re: Error Trapping

Post by HansV »

You have to use some kind of On Error statement, otherwise the code will pause. For example:

Code: Select all

On Error Resume Next
Set objFile = objFSO.GetFile(.FoundFiles(x))
If Err.Number <> 0 Then
  MsgBox Err.Number
  GoTo Skip
End If
On Error GoTo Skip
Note that the check for Err.Number is after the instruction that could cause the error.
Best wishes,
Hans

GeoffW
PlatinumLounger
Posts: 4046
Joined: 24 Jan 2010, 07:23

Re: Error Trapping

Post by GeoffW »

An alternative to error trapping is to use the dir() command to determine if the file exists, before attempting to open it.

User avatar
Don Wells
5StarLounger
Posts: 689
Joined: 27 Jan 2010, 16:45
Location: Ottawa, Ontario, Canada

Re: Error Trapping

Post by Don Wells »

GeoffW wrote:An alternative to error trapping is to use the dir() command to determine if the file exists, before attempting to open it.
I don't think that will help in the instance where the file of interest is in use.
Regards
Don