Function SendOutlookEmail(ByRef olApp As Object, ByRef subject As String, ByRef toRecip() As String, ByRef ccRecip() As String, ByRef bodyText As String) As Boolean
On Error GoTo Err_Handler
Dim objMail As Object
Dim recip As String
Dim i As Integer, numRecips As Integer
Set objMail = olApp.createitem(0) 'OlItemType.olMailItem = 0
With objMail
numRecips = UBound(toRecip)
For i = 1 To numRecips
If LenB(toRecip(i)) > 0 Then
.Recipients.Add toRecip(i) 'OlMailRecipientType.olTo = 1, default though so not required
End If
Next i
numRecips = UBound(ccRecip)
For i = 1 To numRecips
If LenB(ccRecip(i)) > 0 Then
.Recipients.Add(ccRecip(i)).Type = 2 'OlMailRecipientType.olCC = 2
End If
Next i
.BodyFormat = 1 'OlBodyFormat.olFormatPlain = 1
.subject = subject
.Body = bodyText
.send
End With
Set objMail = Nothing
SendOutlookEmail = True
Exit_Handler:
Exit Function
Err_Handler:
If Err.Number < 0 Then 'Normally some large negative number if .send fails
Call MsgBox("A contact is formatted incorrectly. Please correct the email address. Saving email to drafts.", vbExclamation, "Invalid Email Address")
objMail.Save
SendOutlookEmail = True
Else
MsgBox "Error sending email via Outlook."
SendOutlookEmail = False
End If
Resume Exit_Handler
End Function