Error Trap Not Working (Permission Denied)

robherms

Registered User.
Local time
Today, 16:45
Joined
Aug 18, 2006
Messages
15
I have a function that renames and copies files from one directory to another. Here is a sample of the code

Code:
'Set sNextFile to a PDF File in source_directory
sNextFile = Dir$(SOURCE_DIRECTORY + "*.PDF")

On Error GoTo errNext_File

Loop_Start:
'Loop Until all Files in source_directory are read
While sNextFile <> ""
.
.
.
    'Goto Next File
    sNextFile = Dir$
Wend

Exit Sub

errNext_File:
    'Goto Next File
    sNextFile = Dir$
    'MsgBox (Error)
    GoTo Loop_Start
End Sub

My problem is that I am still getting Permission Denied errors within the loop when the following occurs:

Attempting to copy a file, xyz.pdf, to a directory that already contains a file named xyz.pdf and the file to be overwritten is open.

This program is supposed to be running 24 hours a day and it always crashes with a "Permission Denied" error instead of just going to the next fille. For the life of me I cannot figure out why it doesn't just jump to errNext_File. Any help would be greatly appreciated.
Thanks!!
 
Change your error handler to:
Resume Next

Or use the DIR function before trying to copy to see if the file exists. If so, you can bypass (or kill the existing file and copy the new).
 
Change your error handler to:
Resume Next

Or use the DIR function before trying to copy to see if the file exists. If so, you can bypass (or kill the existing file and copy the new).

I would rather not resume next if possible. There are four lines of code - three that copy the file to a new location and a final line that deletes the origional file. If I cannot complete these three copy commands, I do not want to delete the origional file.

Is there anyway to check and see if a file is open?
 
never never never use a goto except for the the line

on error goto

this is a cardinal sin in programming and goes against the concept of structured programming. I've been programming using structured languages for 30+ years and I've never had to use a goto.
 
I think I have come up with a work around...
I am now checking to see if the new file that I want to create already exists. If so, I do not make the copy but rather inform the user that the file exists and it must be deleted before the procedure can continue. This should eliminate any errors that arise from trying to overwrite an open file as well as the goto statement. Thanks for your help!
 
You might also offer to do the delete for them at this time if they wish.
 

Users who are viewing this thread

Back
Top Bottom