Solved

VBA Sendmail error trap

Posted on 2011-09-23
8
458 Views
Last Modified: 2012-05-12
I'm using this function to send order acknowledgements to customers. Unfortunately it crashes if the email address is invalid. How can I change this to send an error (to myself) if it crashes?
I have tried an error trap but it does not seem to work: -

    Set objMessage = CreateObject("CDO.Message")
    objMessage.Subject = Subject
    objMessage.From = FromAddress
   
    objMessage.To = EmailAddress
   
    objMessage.AddAttachment ("file://" & FilePath)
   
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
   
    'Name or IP of Remote SMTP Server
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "192.999.999.999"
   
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 2

    '("http://schemas.microsoft.com/cdo/configuration/sendusername") = "Username"
   
    'objMessage.Configuration.Fields.Item _
    '("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "Passord"
   
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
   
    objMessage.Configuration.Fields.Update
       
    objMessage.Send
0
Comment
Question by:HKFuey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 61

Expert Comment

by:mbizup
ID: 36586661
Try this (see notes in the code):

On Error Goto EH    '<--- add this
   Set objMessage = CreateObject("CDO.Message")
    objMessage.Subject = Subject
    objMessage.From = FromAddress
   
    objMessage.To = EmailAddress
   
    objMessage.AddAttachment ("file://" & FilePath)
   
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
   
    'Name or IP of Remote SMTP Server
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "192.999.999.999"
   
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 2

    '("http://schemas.microsoft.com/cdo/configuration/sendusername") = "Username"
   
    'objMessage.Configuration.Fields.Item _
    '("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "Passord"
   
    objMessage.Configuration.Fields.Item _
    ("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
   
    objMessage.Configuration.Fields.Update
       
    objMessage.Send

'**** Add the following
    Exit Function ' (or Exit Sub)

EH:
   msgbox "ERROR " & err.number & ": " & err.description
0
 
LVL 61

Accepted Solution

by:
mbizup earned 500 total points
ID: 36586737
If you want to notify yourself, you can include that in your error handler:


EH:
   msgbox "ERROR " & err.number & ": " & err.description
docmd.SendObject acSendNoObject,,, "YourEmail@abc.com",,,"Database error", "An email attempt failed, sending to " &  EmailAddress, false
0
 

Author Comment

by:HKFuey
ID: 36587139
Hi mbizup
Thanks for your suggestion, I did try something similar but I will go with your code and see what happens. Only problem is I won't know if it works until it crashes again (which could be next week)  : )
0
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

 
LVL 61

Expert Comment

by:mbizup
ID: 36587177
I'm not in any hurry.., but just as a debugging technique,  can't you deliberately throw in an invalid email address to force it to crash?
0
 

Author Comment

by:HKFuey
ID: 36587180
OK will try but early finish today (Wahey!!) so will be next week.
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36587200
Sounds good - wish I could say the same.    :)

Have a nice weekend.
0
 

Author Closing Comment

by:HKFuey
ID: 36587668
Stayed on for a bit, got it to work! THANKS!
0
 
LVL 61

Expert Comment

by:mbizup
ID: 36587729
Excellent!

Nice way to end the week.
0

Featured Post

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article describes two methods for creating a combo box that can be used to add new items to the row source -- one for simple lookup tables, and one for a more complex row source where the new item needs data for several fields.
Access developers frequently have requirements to interact with Excel (import from or output to) in their applications.  You might be able to accomplish this with the TransferSpreadsheet and OutputTo methods, but in this series of articles I will di…
Using Microsoft Access, learn some simple rules for how to construct tables in a relational database. Split up all multi-value fields into single values: Split up fields that belong to other things into separate tables: Make sure that all record…
With Microsoft Access, learn how to specify relationships between tables and set various options on the relationship. Add the tables: Create the relationship: Decide if you’re going to set referential integrity: Decide if you want cascade upda…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question