Solved

VBA Sendmail error trap

Posted on 2011-09-23
8
466 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
Back Up Your Microsoft Windows Server®

Back up all your Microsoft Windows Server – on-premises, in remote locations, in private and hybrid clouds. Your entire Windows Server will be backed up in one easy step with patented, block-level disk imaging. We achieve RTOs (recovery time objectives) as low as 15 seconds.

 
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

How Blockchain Is Impacting Every Industry

Blockchain expert Alex Tapscott talks to Acronis VP Frank Jablonski about this revolutionary technology and how it's making inroads into other industries and facets of everyday life.

Question has a verified solution.

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

This article will help to fix the below errors for MS Exchange Server 2013 I. Certificate error "name on the security certificate is invalid or does not match the name of the site" II. Out of Office not working III. Make Internal URLs and Externa…
This article shows how to get a list of available printers for display in a drop-down list, and then to use the selected printer to print an Access report or a Word document filled with Access data, using different syntax as needed for working with …
With Microsoft Access, learn how to start a database in different ways and produce different start-up actions allowing you to use a single database to perform multiple tasks. Specify a start-up form through options: Specify an Autoexec macro: Us…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
Suggested Courses

632 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