the OutputTo Action Was Cancelled, runtime error 2501

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I'm using the OutputTo method to export the results of a query to Excel, and
it is working fine so long as the user does not cancel the action when the
filename dialog box displays, at which point an error occurs.

This is what I wrote to ouput the results to Excel:
DoCmd.OutputTo acOutputReport, Me.Export2ExcelList, acFormatXLS, , True

My question is: how can i stop the runtime error?
 
you need to include error handling code in the procedure, such as

On Error GoTo Handle_Err

DoCmd.OutputTo acOutputReport, _
Me.Export2ExcelList, acFormatXLS, , True

Exit_Err:
Exit Sub

Handle_Err:
Select Case Err.Number
Case 2501
' The OutputTo action was cancelled.
Resume Exit_Err
Case Else
Msgbox Err.Number & " " & Err.Description
Resume Exit_Err
End Select

hth
 
Back
Top