What's wrong with my email code?

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

Tony Williams

I have a tick box control that when checked opens Outlook and attaches a
report. If the user clicks send then everything is OK. BUT if the user
clicks the close on Outlook without sending the email I want to trap the
error message 2501 and ask the user what to do. Here is my code so far:
Private Sub Loadtxt_Click()
On Error GoTo Exit_Handler
If Me!Loadtxt = True Then
DoCmd.SendObject acSendReport, "rptOnlineUpload", acFormatRTF, , , ,
"OnLineUpload", "Please upload this document to Online", True
End If
Exit_Handler:

If Err.Number = 2501 Then
If vbCancel = MsgBox("You have not sent the email, click OK to send or
Cancel to cancel the message", vbOKCancel, "Email not sent") Then
Cancel = True
Else
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
Resume Exit_Handler
End If
End If
End Sub

If they close Outlook without sending the message then Outlook closes and no
message pops up and the email isn't sent. It just closes whether or not they
have filled in an email address. If they click Cancel then Outlook closes
but the tick box is still checked which it shouldn't be because the email
hasn't gone.
Can anyone help me here?
I did get some of the code from a suggestion from "Brad" but couldn't get
his suggestion to do what I wanted either (Sorry Brad)
TIA Tony Williams
 
I think (but am not sure) that sendobject only returns an error if it fails
to pass the email to your email client (eg. Outlook). Once the email has got
into that client, I think that sendobject is satisfied, & has no further
knowledge of what happens (or does not happen) to that email within the
email client.

On reflection, it would have to be like that. What if sendobject passed the
email to your email client, but then, you did not send the email (from that
client) until 30 days later? Would you expect the sendobject call to remain
"in limbo", for 30 days, until you actually sent the email, so it could then
return "no error"? Clearly this is not gonna happen!

HTH,
TC
 
Thanks TC. What is happening when Outlook is closed I get my popup message
box that asks the user if they want to send the email or cancel. if they
cancel that's OK if they click Ok that they want to send the email I get the
error message 2501 the sendObject was cancelled. Is that because outlook has
been closed. Is there no way I can get Outlook to open again if they click
OK?
Tony
 
Tony, I'm not sure that I understand your description. Do you really mean
that when you >cancel< the popup, the sendobject succeeeds, but when you
ok< the popup, you get runtime error 2501?

Please tell me the exact text of the popup message. Does it say something
like: "A program is trying to send an email on your behalf, do you want to
allow this?"

TC
 
Hi TC thanks for sticking with me on this.
My tick box opens Outlook and attaches a report. If the user decides they
don't want to send the email and closes Outlook I have created a popup
message box that asks the user if they are sure they want to cancel the
email or do they still want to save it. If they click Cancel then Outlook
closes and the tickbox beomes unticked, that's fine. However if they tick OK
on my message box then they go back to Outlook. BUT if they try to close
Outlook again without sending the email that's when I get the 2501 message
saying SendObject closed.

I have for the time being done a work around that actually doesn't give them
any option just tells them the message hasn't gone and cancels the tickbox.
here is the code:

On Error GoTo Exit_Handler
If Me!Loadtxt = True Then
DoCmd.SendObject acSendReport, "rptOnlineUpload", acFormatRTF, , , ,
"OnLineUpload", "Please upload this document to Online", True

Exit_Handler:
If Err.Number = 2501 Then
If vbOK = MsgBox("You have not sent the email, click OK to cancel
the message", vbOKOnly, "Email not sent") Then
Cancel = True
Me!Loadtxt = False
Else
MsgBox Err.Description, vbExclamation, "Error No: " & Err.Number
End If
Resume Exit_Handler
End If
End If

Ideally I would like the user to be able to use my popup message box to have
a choice with a Cancel/OK buttons to either cancel the email on closing
Outlook or if they have attempted to close Outlook by mistake be able to
cancel my popup message box and send the email.

Hope I've explained that OK?
Tony
 
Tony, sorry, but it's still not clear to me, what you are doing. Perhaps I'm
having an "off day"!

Is this correct:

- you have a tick box on an Access form;

- when you tick that box, an event (say AfterUpdate) of that tickbox uses
Sendobject to send an email with an attached report, to Outlook;

- Outlook opens & displays that email;

What happens then? Please spell it out clearly, point by point, as shown
above. In particular, if you are running any of this code >from Outlook<,
ie. in >Outlook< VBA, please make that clear.

Cheers,
TC
 
Really sorry TC, I think it's a bit of wood for the trees!!! So here goes

1.Outlook opens with email attachment, giving the user the option to enter
addressee and edit content

2.If user clicks Send, email goes OK and Outlook closes. So no problems

3. BUT if user closes Outlook without sending email by clicking the close X
in the top right hand corner, I was getting error 2501 sendobject was
cancelled.
So I put the error trapping in to catch the error and show my own message.
"The email hasn't gone. Click cancel to cancel the email and close
Outlook and OK if you want to send the email"
If they clicked Cancel, no problem, If they clicked OK Iwanted them to
return to the Outlook window they were in the process of closing but I was
getting the error 2501 message even though the error trapping code was
there!

I'm sure it must be something to do with nested IF statements and getting
them in the right order but i couldn't figure it out.

Hope that is a bit clearer TC
Thanks for your patience
Tony
 
Sorry for the late reply Tony, I've been busy for the last few days.

Let's finally spell out your process, in full.

(1) You have a tick box on an Access form.

(2) When you tick that box, an event (say AfterUpdate) of that tickbox uses
Sendobject to send an email with an attached report, to Outlook.

(3) Outlook opens with email attachment, giving the user the option to enter
addressee and edit content.

(4) If user clicks Send, the email goes OK, Outlook closes, >>and then the
SendObject call returns, correctly, with no error?<<.

(5) BUT if the user closes Outlook without sending the email, by clicking
the close X in the top right hand corner, the SendObject returns with error
2501 (the sendobject call was cancelled). You trap that error and display
your own message, "The email hasn't gone. Click cancel to cancel the email
and close Outlook and OK if you want to send the email"

(6) If they OK that message, you want them to return to the Outlook window
they were in the process of closing. But this doesn't work because you get
error 2501 again?

So in (6), >how< you try to get them to "return to the Outlook window"? Do
you issue another sendobject call? I imagine you should do something like
this (in pseudocode):

AGAIN:
sendobject
if error 2501
msg "The email hasn't gone .. etc."
if they click:
Cancel: goto DONE
OK: goto AGAIN
endif
DONE:

Does that help?

TC
 
Thanks TC and don't apologise for the delay I'm just glad you're still hanging in there with me!
I've gone thru your email and you have got the sequence spot on. So am I right in thinking that I now need to interpret you pseudocode into a proper procedure? If so could you give me a few pointers there. I'm a newbie at VBA (as you probably guessed) and the other excuse I've got is that at my ripe old age of 60 the white cells aren't as active as they used to be. For instance do I have to declare the DONE and AGAIN statements first?
Thanks again TC
Tony
 
Hi Tony

Yes, you would need to implement the pseduocode as proper VBA. Here's one possible way (UNTESTED). Note that I am using a GoTo statement to "try again". GoTo statements are generally considered to be bad programming practice, except in a few cases, of which this is not one! But let's just get something working, for now!

Pseudocode:
AGAIN:
sendobject
if error 2501
msg "The email hasn't gone .. etc."
if they click:
Cancel: goto DONE
OK: goto AGAIN
endif
DONE:

VBA: (UNTESTED)

AGAIN:
on error resume next
docmd.sendobject ... your parameters here ...
select case err.number
case 0 ' no error.
case 2501 ' email cancelled!
if msgbox ("Didn't go. Try again?", vbyesno) = vbyes then goto again
case else
msgbox "Unexpected error " & err.number & ": " & err.description
end select
on error goto 0

Give that a whirl & see how it goes.

Congrats on the 60! I wish I was young enough to say, "You're old enough to be my father!" :-)

Cheers,
TC

Thanks TC and don't apologise for the delay I'm just glad you're still hanging in there with me!
I've gone thru your email and you have got the sequence spot on. So am I right in thinking that I now need to interpret you pseudocode into a proper procedure? If so could you give me a few pointers there. I'm a newbie at VBA (as you probably guessed) and the other excuse I've got is that at my ripe old age of 60 the white cells aren't as active as they used to be. For instance do I have to declare the DONE and AGAIN statements first?
Thanks again TC
Tony
 
Back
Top