Error Handling

Snowflake68

Registered User.
Local time
Today, 03:51
Joined
May 28, 2014
Messages
464
I have found a thread on here with the code below which I am using (Thank you 'stopher') However I would like to add in something that checks to see if the email was closed without sending and then pop up a message just informing the user of this.

Code:
Public Sub SendEmail()

    Dim appOutLook As Object
    Dim MailOutLook As Object
        
    'create the two reports temporarily
    DoCmd.OutputTo acOutputReport, "Report1", acFormatPDF, "c:\someLocation\Report1.pdf", False
    DoCmd.OutputTo acOutputReport, "Report2", acFormatPDF, "c:\someLocation\Report2.pdf", False
    
    'assign our object references
    Set appOutLook = CreateObject("Outlook.Application")
    Set MailOutLook = appOutLook.CreateItem(olMailItem)
        
    With MailOutLook
        'set the recipient list
        .To = "someone@somewhere.com"
        
        'set the subject
        .Subject = "My Reports"
        
        'set the body text
        .body = "Here are the reports in pdf format"
        
        'add the reports we created
        .attachments.Add "c:\someLocation\Report1.pdf"
        .attachments.Add "c:\someLocation\Report2.pdf"
        
        'send the email
        .Send
    End With
    
    'tidy up..
    
    'get rid of our object references
    Set appOutLook = Nothing
    Set MailOutLook = Nothing

    'delete our temporary files
    Kill "c:\someLocation\Report1.pdf"
    Kill "c:\someLocation\Report2.pdf"

End Sub

Ive tried adding in this code which I also found with some code for SendObject which works on the SendObject code but not on the 'With MailOutLook' code. It doesnt produce any errors but doesnt pop up the message when I close the email either. It just prompts so see if I want to save the email.

Can anyone advise me please?

Code:
    On Error GoTo cmdEmail_Click_Err

'code goes here

cmdEmail_Click_Exit:
    Exit Sub

cmdEmail_Click_Err:
    MsgBox "Email NOT sent for quote ", vbCritical, "Email Quote"
    Resume cmdEmail_Click_Exit
 
Last edited:
I don't think there anything you can do directly.

you can error trap processes before the .send
at that point you are asking outlook to send the email you created.

the easiest way is to check in outlook.

you may be able to interrogate the outbox, or the send folder, but I don't know how you would uniquely identify a particular email.

I tend to use .save rather than .send, which saves it in drafts - and then send the draft manually, but it depends on what you are trying to do
 
I think if you use .Send rather than .Display you will get an outlook error if there is a problem.

To try and prove a point, substitute a dummy bad email address in the code flibble.bademailaddress and see what you get...
 
I think if you use .Send rather than .Display you will get an outlook error if there is a problem.

To try and prove a point, substitute a dummy bad email address in the code flibble.bademailaddress and see what you get...

I have done what you said and I get the Allow/Deny message that Outlook is trying to send an email to which I click Deny and then I get the pop up message.

But this isn't what I want as I need the user to see the email before sending but if they close it then that is when I want the message to pop up.
 
I don't think there anything you can do directly.

you can error trap processes before the .send
at that point you are asking outlook to send the email you created.

the easiest way is to check in outlook.

you may be able to interrogate the outbox, or the send folder, but I don't know how you would uniquely identify a particular email.

I tend to use .save rather than .send, which saves it in drafts - and then send the draft manually, but it depends on what you are trying to do

Thanks but I need the email to pop up so that the user can add their own message. (dont want them to have to go into drafts to find it). But thanks for your reply.
 
In that context, there is no way to tell from the one call that you didn't sent the message you displayed. My strategy would be to include some sort of message number in the subject line and then search the Sent folder for a subject with that number.
 
Maybe the post at the bottom of this thread will help:
http://www.access-programmers.co.uk/forums/showthread.php?t=199135

I haven't tried it myself.

Thanks stopher. I just tried it and just placed all of the code before the line that says

Code:
With MailOutLook

but I got an error "label not defined" and points to this line in the code,

Code:
 On Error GoTo mailErr:

but to be honest I dont know where in my code to put it or if I need to change any references. Are you able to help please?
 
Last edited:
Thanks stopher. I just tried it and just placed all of the code before the line that says

Code:
With MailOutLook

but I got an error "label not defined" and points to this line in the code,

Code:
 On Error GoTo mailErr:

but to be honest I dont know where in my code to put it or if I need to change any references. Are you able to help please?


You should have some error trapping in your subroutine.?
It is generally in the format of

ExitRoutine:
<code to close objects>

Err_handler
<Msg to display error>
Resume ExitRoutine

In fact the code you refer to above has

Code:
cmdEmail_Click_Exit:
    Exit Sub

cmdEmail_Click_Err:
    MsgBox "Email NOT sent for quote ", vbCritical, "Email Quote"
    Resume cmdEmail_Click_Exit

so you just need to match up your go to with your correct label.?
 
the problem is that outlook failing to send an email may not raise an access error ....

can stopher confirm whether it does?

it may raise a trappable runtime error that the form error event would pick up. Again, I am not sure.
 
the problem is that outlook failing to send an email may not raise an access error ....

can stopher confirm whether it does?

it may raise a trappable runtime error that the form error event would pick up. Again, I am not sure.
The point is if you try to read one of the mail objects properties then either..

the object no longer exists and therefore an error is thrown. If the object no longer exists then it has been sent. So detecting an error means it has been sent.

Or

a result is given when reading a property of the object (no error occurs) i.e. the object still exists. Therefore it has not been sent.

I don't have outlook loaded but the code given in the link follows the principle.

It's a bit kludgy but I guess it works given the comments made in the link.
 
You should have some error trapping in your subroutine.?
It is generally in the format of

ExitRoutine:
<code to close objects>

Err_handler
<Msg to display error>
Resume ExitRoutine

In fact the code you refer to above has

Code:
cmdEmail_Click_Exit:
    Exit Sub

cmdEmail_Click_Err:
    MsgBox "Email NOT sent for quote ", vbCritical, "Email Quote"
    Resume cmdEmail_Click_Exit

so you just need to match up your go to with your correct label.?

Thanks Gasman but its the suggested code from the post that Stopher pointed me at that Im trying to use but just dont know where to put it.

Code:
oMsg.Display True    'make outlook modal
  
  'check if the email was sent or just closed
  On Error Resume Next
  bSent = oMsg.sent            'just used to get a error, 
  If Err = 0 Then
      'no message, email closed, oMsg still exists
      'no message, email saved, oMsg still exists
      bSent = False
  Else
      'message sent, oMsg is null
      bSent = True
  End If
  On Error GoTo mailErr:

Ive been trying and trying all day but just cannot get it to work :banghead:
 
the problem is that outlook failing to send an email may not raise an access error ....

can stopher confirm whether it does?

it may raise a trappable runtime error that the form error event would pick up. Again, I am not sure.

Spot on, that is the trouble. It doesn't produce an error when you just close the email, you just get asked in you want to save your changes.

Really stuck on this now :(
 

Users who are viewing this thread

Back
Top Bottom