The e-mail module, once it is imported together with the log module (all the modules require the log module, since all the modules generate log), abstracts the complexity of sending an Oulook e-mail in one simple and straightforward function. Of course sending an e-mail in VBA is not the most complex task you will ever perform, but it is complicated enough to be simplified as it is in this module. So please download the module itself (the full code is depicted below) or download the workbook that contains it (and a lot more).
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'DOWNLOAD
'File Name = EMAIL_MODULE.bas
'Link to donwload:
' https://www.dropbox.com/s/p5lknx9qr6mwl9a/EMAIL_MODULE.bas?raw=1
'File Name= VBA_MODULES_43.xlsm (file that has the log_module and the email_module ready to be used)
'Link to donwload:
' https://www.dropbox.com/s/pxxwgap8hl7m9xw/VBA_MODULES_43.xlsm?raw=1
'Raw Code:
'----------------------------------------------------------------------------------------------------------------------
'Option Explicit
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' @module EMAIL_MODULE
' @author Yan França Tosta
' @version 43
' @since 06/2015
' @requires the LOG_MODULE
'
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' @function: Send_Email as boolean
' @description: sends e-mail using outlook application
' @param ByVal Receiver As String: the recipient of the e-mail
' @param ByVal Subject As String: the subject of the e-mail
' @param Optional ByVal Message As String = vbNullString: the HTML message body of the e-mail
' @param Optional ByVal CC As String = vbNullString: the recipients in copy
' @param Optional ByVal File_Path As String = vbNullString: the attachment file path
' @param Optional ByVal Display As Boolean = False: if true, displays the send screen; sends the e-mail automatically otherwise
' @return Boolean: true if success, false otherwise
'
Public Function Send_Email(ByVal Receiver As String, _
ByVal Subject As String, _
Optional ByVal Message As String = vbNullString, _
Optional ByVal CC As String = vbNullString, _
Optional ByVal File_Path As String = vbNullString, _
Optional ByVal Display As Boolean = False) As Boolean
Dim outlookApplication As Object
Dim outlookMail As Object
Dim function_name As String
function_name = "EMAIL_MODULE.Send_Email"
LOG_MODULE.LOG_PRINT function_name
LOG_MODULE.LOG_PRINT function_name, "Sending mail with subject '" _
& Subject & "' to '" & Receiver & "'."
On Error GoTo error_1000
LOG_MODULE.LOG_PRINT function_name, "Checks whether the Outlook is open. If it is not, it tries to opens a new instance"
Set outlookApplication = GetObject(, "Outlook.Application")
If outlookApplication Is Nothing Then
Set outlookApplication = CreateObject("Outlook.Application")
End If
Set outlookMail = outlookApplication.CreateItem(0)
With outlookMail
.To = Receiver
.CC = CC
.Subject = Subject
.HTMLBody = Message
If Not File_Path = vbNullString Then
.Attachments.Add File_Path
End If
If Display Then
LOG_MODULE.LOG_PRINT function_name, "Opening Display to Send the e-mail"
.Display
Else
LOG_MODULE.LOG_PRINT function_name, "Sending the e-mail without opening the display"
.send
End If
End With
Send_Email = True
LOG_MODULE.LOG_PRINT function_name, "E-mail sent"
LOG_MODULE.LOG_PRINT function_name, 0
Exit Function
error_1000:
LOG_MODULE.LOG_PRINT function_name, Err.Description
LOG_MODULE.LOG_PRINT function_name, 1
Send_Email = False
Exit Function
End Function
'
' @function: Send_Email_Display as boolean
' @description: displays the outlook send e-mail screen
' @return Boolean: true if success, false otherwise
'
Public Function Send_Email_Display() As Boolean
Dim function_name As String
function_name = "EMAIL_MODULE.Send_Email_Display"
LOG_MODULE.LOG_PRINT function_name
On Error GoTo error_1000:
Send_Email_Display = Send_Email(vbNullString, vbNullString, vbNullString, vbNullString, vbNullString, True)
LOG_MODULE.LOG_PRINT function_name, 0
Exit Function
error_1000:
LOG_MODULE.LOG_PRINT function_name, Err.Description
LOG_MODULE.LOG_PRINT function_name, 1
MsgBox "Unable to open the e-mail display", , "ERROR"
Send_Email_Display = False
Exit Function
End Function
Nenhum comentário:
Postar um comentário