Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VBA Sendmail error trap

Posted on 2011-09-23
8
Medium Priority
?
482 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
  • 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 2000 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
Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Vote for the Most Valuable Expert

It’s time to recognize experts that go above and beyond with helpful solutions and engagement on site. Choose from the top experts in the Hall of Fame or on the right rail of your favorite topic page. Look for the blue “Nominate” button on their profile to vote.

Question has a verified solution.

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

This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
In a use case, a user needs to close an opened report by simply pressing the Escape (Esc) key. This can be done by adding macro code in Report_KeyPress or Report_KeyDown event.
In Microsoft Access, learn how to use Dlookup and other domain aggregate functions and one method of specifying a string value within a string. Specify the first argument, which is the expression to be returned: Specify the second argument, which …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Suggested Courses

886 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