Outlook Email

Vishesh's picture

Copy the following code in a general module and call SendEmail function. It returns 0 if successfull else error number is returned.

Enum OptSendDisplay
    Send = 1
    Display = 2
End Enum
Public Function SendEmail(strMailTo As String, strSubject As String,  _
    strBodyText As String, SendOrDisp As OptSendDisplay, blnReceipt As  _
    Boolean, Optional strAttachment As String, Optional strCCTo As String,  _
    Optional strBCCTo As String) As Long
'This will return 0 if successfull else error number

    Dim objApp                  As Object
    Dim objMail                 As Object
    Dim arrAttachment()         As String
    Dim intAttachLoop           As Integer
    On Error GoTo ErrHandler
    Set objApp = CreateObject("Outlook.Application")
    Set objMail = objApp.CreateItem(0)
    With objMail
        .To = strMailTo
        If Len(strCCTo) > 0 Then .CC = strCCTo
        If Len(strBCCTo) > 0 Then .BCC = strBCCTo
        .Subject = strSubject
        .Body = strBodyText
        .ReadReceiptRequested = blnReceipt
        If Len(strAttachment) > 0 Then
            arrAttachment() = split(strAttachment, ",")
            For intAttachLoop = 0 To UBound(arrAttachment, 1)
                .Attachments.Add arrAttachment(intAttachLoop)
            Next intAttachLoop
        End If
        If SendOrDisp = Display Then .Display
        If SendOrDisp = Send Then .Send
    End With
    SendEmail = 0 'Success returned
    Set objApp = Nothing
    Set objMail = Nothing
    Exit Function
    SendEmail = Err.Number 'Error returned
    GoTo exitSendAttachment
End Function
Sub Test()
    If SendEmail("Email@abc.com", "Test Mail - Delete", "Body Text", Send, True) = 0 Then
        MsgBox "Success"
        MsgBox "Failed"
    End If
End Sub


This code worked fine with outlook. Will this work if I have Lotus Notes on my instead of Outlook. Or another code is required ?

How can we attach multiple files in the same mail in this code ?