Sending emails from Access

  • Thread starter Thread starter Tony Williams
  • Start date Start date
T

Tony Williams

I have a form which has a tick box control which has SendObject code to
email a report. The code is
Private Sub Loadtxt_Click()
On Error GoTo Err_Handler
DoCmd.SendObject acSendReport, "rptOnlineUpload", acFormatRTF, , , ,
"OnLineUpload", "Please upload this document to Online", True
Exit_Handler:
Exit Sub

Err_Handler:
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler
End If
End Sub
This works fine except that if the user closes Outlook and doesn't send the
email I get an error message that says error 2501 The send Object was
cancelled
Is there anyway I can code a message that is a little more user friendly
like "Are you sure you dont want to send the email" with YES/NO buttons and
if YES they stay in Outlook and if No they close Outlook without another
error message
TIA
Tony Williams
 
In your err handler try something like this:

Err_Handler:

Dim response As Integer
if err.number = 2501 then
response = msgbox("Oops", "Outlook is not open, please start the program and
try again", vbokonly) ' (whatever the syntax is for message boxes)
else
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
end if
Resume Exit_Handler

if the error number is 2501 it should launch a Message box telling the user
to open Outlook, then exit. If the error number is something else it will
display a message box with the error number and description. Giving you a
trap to catch the number and ability to write around it. You could program
response to retry the code or exit dependant on the response to the mesage
box.

for example
Instead of using a vbokonly messaeg box, use a vbokcancel, if response=vbOK
then Loadtxt_Click() else resume exit_handler

if err.num=2501 then
response = msgbox("Oops", Start Outlook then click "OK", or click Cancel to
Exit", vbOKCancel)
else
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
response = 0
end if

if response=vbOK then
Loadtxt_Click()
else
Resume Exit_Handler
end if




Hope this helps or at least gives you some ideas.
Brad
 
Thanks Brad that was really useful.
Tony
Brad said:
In your err handler try something like this:

Err_Handler:

Dim response As Integer
if err.number = 2501 then
response = msgbox("Oops", "Outlook is not open, please start the program and
try again", vbokonly) ' (whatever the syntax is for message boxes)
else
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
end if
Resume Exit_Handler

if the error number is 2501 it should launch a Message box telling the user
to open Outlook, then exit. If the error number is something else it will
display a message box with the error number and description. Giving you a
trap to catch the number and ability to write around it. You could program
response to retry the code or exit dependant on the response to the mesage
box.

for example
Instead of using a vbokonly messaeg box, use a vbokcancel, if response=vbOK
then Loadtxt_Click() else resume exit_handler

if err.num=2501 then
response = msgbox("Oops", Start Outlook then click "OK", or click Cancel to
Exit", vbOKCancel)
else
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
response = 0
end if

if response=vbOK then
Loadtxt_Click()
else
Resume Exit_Handler
end if




Hope this helps or at least gives you some ideas.
Brad
 
Back
Top