On Error Resume Next....error

Monsora83

Registered User.
Local time
Today, 15:19
Joined
May 16, 2011
Messages
41
I am trying to delete a file Text.txt, if the file is open it will return an error and skip the delete command.

My error trapping is set to 'Break on unhandled errors'.

x="C:\Text.txt"

On Error Resume Next
Kill x
On Error Goto 0

I am boggled at why this doesnt work. Any ideas?
 
I am trying to delete a file Text.txt, if the file is open it will return an error and skip the delete command.

My error trapping is set to 'Break on unhandled errors'.

x="C:\Text.txt"

On Error Resume Next
Kill x
On Error Goto 0

I am boggled at why this doesnt work. Any ideas?

The On Error Resume Next statement cancels the error state before continuing, so the error will not be reported.

The On Error Goto 0 is not executed, but instead replaces the On Error Resume Next and will control the remainder of the code in the Function unless it also is replaced.

Removing the On Error Resume Next statement, and moving the On Error Goto 0 to before the Kill statement could give you what you are looking for.

-- Rookie
 
Last edited:
The On Error Resume Next statement cancels the error state before continuing, so the error will not be reported.

The On Error Goto 0 is not executed, but instead replaces the On Error Resume Next and will control the remainder of the code in the Function unless it also is replaced.

Removing the On Error Resume Next statement, and moving the On Error Goto 0 to before the Kill statement could give you what you are looking for.

-- Rookie

I see I should have written my situation a bit clearer.

The code I am running imports the data from Text.txt, after it imports the data it will delete the file. If someone else is looking at the file then I want the code to skip the delete command (and since the file will be open if someone is reading it, it is unable to delete the file, so it will give me an error, and using the On Error Resume Next it will just end up skipping the delete command) and move on with the code without me receiving an error.

Then I reset the On Error to On Error GoTo 0 to catch any unseen errors later in my code.

And my problem is that even when I use the On Error Resume Next to suppress the error I should receive if someone has it open, I still recieve a 'Runtime Error 70' when I execute the Kill x command with how it is written above.

Hope that clears up my horribly written initial question.
 
I tried what you are doing while having the file open in notepad. The Kill command successfully deleted the file whether or not I had it open, and it didn't matter if I had On Error code in effect because there was no error. Runtime Error 70 means "Permission Denied", so I would suggest something else is in effect not allowing you to delete the file. Perhaps you're not set up as an administrator on your PC?

You can do quick experiments by creating the following subroutine in a code module, then entering "call deletetest" from the Immediate pane:

Code:
Sub DeleteTest
    kill "c:\text.txt"
End Sub
 
Just handle the error in the error handler if the error is a 70 it will just Resume Next:
Code:
ExitProc:
Exit Sub
 
Err_handler:
   If err.Number <> 70 Then
      Msgbox Err.Description, vbExclamation, "Error #: " & Err.Number
      Resume ExitProc
      Resume
   Else
      Resume Next
   End If
 
End Sub
 

Users who are viewing this thread

Back
Top Bottom