I have the following code for sending e-mails out. Sometimes the document being sent need to be sent to 2 e-mails (most time there is just 1 e-mail). If there are 2 e-mails in the current code, it will error out because Outlook doesn't immediately recognize the 2 e-mails together (separated b a semicolon). However, if there is 1 e-mail, the code runs and the e-mail sends immediately. I think this is a problem with Outlook because I have all the e-mails stored as contacts.
I found a way around this problem by having the 2nd e-mail be the CC. However, if there is not a 2nd e-mail, then the code errors because there must be a recognizable e-mail in the CC field.
Does anyone know some code that will allow the code to complete regardless if there is 1 or 2 e-mails?
Code:
Public Sub SendBoth_Click()Dim strDocName As String
Dim strWhere As String
strDocName = "ex"
strWhere = "[Deal ID]=" & Me.Deal_ID
DoCmd.ApplyFilter , strWhere
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.Recipient
Dim objOutlookAttach As Outlook.Attachment
Dim fiLename As String, todayDate As String
Dim Invoice1 As String, Invoice2 As String
Invoice1 = Forms!ex.[Confirmation Contact - E-mail]
Invoice2 = IIf(Not IsNull(Forms!ex.[Confirmation Contact - E-mail2]), "; " & Forms!ex.[Confirmation Contact - E-mail2], "")
'Export report in same folder as db with date stamp
todayDate = Format(Date, "mm.dd.yy")
fiLename = "C:\Users\..."
DoCmd.OutputTo acForm, "ex", acFormatPDF, fiLename, False
' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")
' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
With objOutlookMsg
' Add the To recipient(s) to the message.
Set objOutlookRecip = .Recipients.Add(Invoice1 & Invoice2)
objOutlookRecip.Type = olTo
' Add the CC recipient(s) to the message.
'Set objOutlookRecip = .Recipients.Add(Invoice2)
'objOutlookRecip.Type = olCC
' Set the Subject, Body, and Importance of the message.
.Subject = "Document"
.BodyFormat = olFormatHTML
.HTMLBody = "<p style='font-size:11pt;font-family:Arial'>" & "Dear " & </p>"
.Attachments.Add fiLename
' Add attachments to the message.
' If Not IsMissing(AttachmentPath) Then
' Set objOutlookAttach = .Attachments.Add(AttachmentPath)
' End If
' Resolve each Recipient's name.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next
' Should we display the message before sending?
.Send
End With
Set objOutlook = Nothing
DoCmd.ShowAllRecords
DoCmd.SetOrderBy "[Deal ID] " & "DESC"
End Sub