Send email code issue

mari_hitz

Registered User.
Local time
Today, 14:35
Joined
Nov 12, 2010
Messages
120
Hi everyone,

Hope you can help me. I have a database that sends emails trough outlook, the code works fine, however when the outlook does not recognize the email (because there is no person with that email in the organization that I am in) the macro stops and I have to start all over again. (Previous emails are sent, but the ones after the email that is not recognized are not).
Is there any way to indicate to the code that if this happens avoid the error and continue with the loop?

Here is my code:

Code:
Private Sub Command133_Click()

Dim MyDB As Database
  Dim MyRS As Recordset
  Dim MyForm As Form
  Dim objOutlook As Outlook.Application
  Dim objOutlookMsg As Outlook.MailItem
  Dim objOutlookRecip As Outlook.Recipient
  Dim TheAddress As String
  Set MyDB = CurrentDb
  Set MyRS = MyDB.OpenRecordset("Comunicacion 2")
  MyRS.MoveFirst
 
 
  ' Create the Outlook session.
  Set objOutlook = CreateObject("Outlook.Application")
  
  Do Until MyRS.EOF
  ' Create the e-mail message.
  Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
  TheAddress = MyRS![Enterprise]
  

     With objOutlookMsg
        ' Add the To recipients to the e-mail message.
        Set objOutlookRecip = .Recipients.Add(TheAddress)
        objOutlookRecip.Type = olBCC

        ' Set the Subject, the Body, and the Importance of the e-mail message.
        .To = MyRS![Enterprise]
        .Subject = " URGENT ACTION REQUIRED - US Immigration Information Requestt"
        .HTMLBody = "<html><body><font face=calibri> Testing</font></body></html>"
        
        .Importance = olImportanceHigh  'High importance
        ' Resolve the name of each Recipient.
        For Each objOutlookRecip In .Recipients
           objOutlookRecip.Resolve
           If Not objOutlookRecip.Resolve Then
             objOutlookMsg.Display
           End If
        Next
        .Send
      End With
      MyRS.MoveNext
   Loop
   Set objOutlookMsg = Nothing
   Set objOutlook = Nothing
End Sub
 
What you need is a proper Error Handler.. However I am interested in knowing the process of how you categorize/recogonise if an email is invalid?

On a side note, the above code is a VBA procedure not a Macro..
 
Hi pr2-eugin,

Thanks for the data you provided. I am taking a look right now.
At my company we use outlook and we have emails which are called enterprise IDs. My name is Marina Silva, so my enterprise ID is marina.silva@companyname.com. Since the company has an address book, all the enterprise IDs are in that book. So, the code what it does is to take this enterprise and if the sames are in the address book the email is sent, if not, an error from outlook appears that the email is not recognized and the email is not sent and the VBA procedures is ended.

I am sorry I have confused the macro with VBA procedure, it was not my intention.

One last question, the Error handling, should I put it before the code where prepares the email or when it gives the statement .send?

Thanks in advance for all your help.
 
Okay what is "Comunicacion 2"?
So, the code what it does is to take this enterprise and if the sames are in the address book the email is sent, if not, an error from outlook appears that the email is not recognized and the email is not sent and the VBA procedures is ended.
Well you have tried to resolve, but the condition to check you have is..
Code:
If Not objOutlookRecip.Resolve Then
Where it should read as
Code:
If Not objOutlookRecip.Resolve[COLOR=Red][B]d[/B][/COLOR] Then
Also does the error come up in Access or Outlook??
I am sorry I have confused the macro with VBA procedure, it was not my intention.
I am sure you did not do it on purpose, just pointed it out, just to make sure that you do understand the difference.
One last question, the Error handling, should I put it before the code where prepares the email or when it gives the statement .send?
If you check the link I provided it will teach you how exactly how you should have it..

I normally have three parts in my Error Handler Routine,
Error Statement - On Error GoTo someLableToHandle.
ExitProcedureLbl - Normal Exit also comes along.
someLableToHandle - Display Error, Resume ExitProcedureLbl unless there is a special case then ignore..

Putting them together..
Code:
Private Sub someProc()
    On Error GoTo errHandler
   [COLOR=Teal] 'normal Code[/COLOR]
ExitCodeLbl:
    Exit Sub
errHandler:
    [COLOR=Teal]'Check a SPECIAL condition if you want to resume execution, else exit.[/COLOR]
    If Err.Number <> 2501 Then
        Call MsgBox("Following Error Occurred, Exiting Sub : " & Err.Number & " : " & Err.Description, vbCritical)
        Resume ExitCodeLbl
    Else
       [COLOR=Teal] 'Do something.[/COLOR]
    End If
End Sub
 
Your help has been great! I am going to try that out. Have a great day ahead of you!
 

Users who are viewing this thread

Back
Top Bottom