View Single Post
  #2   Report Post  
Posted to microsoft.public.excel.programming
joel joel is offline
external usenet poster
 
Posts: 9,101
Default Set Error handling INSIDE error-trap

Add a new variable to your macro call fileopen and initilize it to false

fileopen = false


Then when you open the file set it to true. When you close the file set it
false again. In you error code make the following change

ErrorTrap:
MsgBox "The Macro has encountered an unforeseen problem" & vbCr & "and
cannot continue", vbCritical, "Error " & Err
Application.Calculation = xlCalculationAutomatic

Err.Clear

' and close the data file
if fileopen then
DataFile.Close SaveChanges:=False
end if


"Michelle" wrote:

I am having a problem with my error trap.

I want to have my error-trap simplydisplay a message and close the data-file
that's being used. However, if the error is generated 'late-on' in my macro,
the data-file may already be closed, so to handle this, I am usingthe
following code:

'===========================
Exit Sub

ErrorTrap:
MsgBox "The Macro has encountered an unforeseen problem" & vbCr & "and
cannot continue", vbCritical, "Error " & Err
Application.Calculation = xlCalculationAutomatic

Err.Clear
On Error Resume Next ' in case the sheets are already protected or
data file is already closed

' and close the data file
DataFile.Close SaveChanges:=False
'===========================

but the 'On Error Resume Next ' line seems to be ignored because it
generates a standard VB error (with the Debug-box displayed). Is this
because I am in an error trap? is there a way around this?

thanks

M