Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

VBA Sendmail error trap

Posted on 2011-09-23
8
Medium Priority
?
476 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 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
Veeam Task Manager for Hyper-V

Task Manager for Hyper-V provides critical information that allows you to monitor Hyper-V performance by displaying real-time views of CPU and memory at the individual VM-level, so you can quickly identify which VMs are using host resources.

 
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

10 Questions to Ask when Buying Backup Software

Choosing the right backup solution for your organization can be a daunting task. To make the selection process easier, ask solution providers these 10 key questions.

Question has a verified solution.

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

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 …
Are you an Exchange administrator employed with an organization? And, have you encountered a corrupt Exchange database due to which you are not able to open its EDB file. This article will explain all the steps to repair corrupt Exchange database.
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…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
Suggested Courses

660 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