Solved

VBA Sendmail error trap

Posted on 2011-09-23
8
435 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
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 61

Expert Comment

by:mbizup
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

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

Expert Comment

by:mbizup
Comment Utility
Sounds good - wish I could say the same.    :)

Have a nice weekend.
0
 

Author Closing Comment

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

Expert Comment

by:mbizup
Comment Utility
Excellent!

Nice way to end the week.
0

Featured Post

Don't lose your head updating email signatures!

Do your end users still have the wrong email signature? Do email signature updates bore you or fill you with a sense of dread? You can make this a whole lot easier on yourself by trusting an Exclaimer email signature management solution. Over 50 million users do...so should you!

Join & Write a Comment

Find out how to use Active Directory data for email signature management in Microsoft Exchange and Office 365.
Local Continuous Replication is a cost effective and quick way of backing up Exchange server data. The following article describes the steps required to configure Local Continuous Replication. Also, the article tells you how to restore from a backup…
To show how to generate a certificate request in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.:  First we need to log into the Exchange Admin Center. Navigate to the Servers >> Certificates…
This video discusses moving either the default database or any database to a new volume.

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now