Function SendOutlookMessage(Recipients As String, Subject As String, Body As String, DisplayMsg As Boolean, Optional CopyRecipients As String, Optional BlindCopyRecipients As String, Optional Importance As Integer = 2, Optional AttachmentPath, Optional AttachmentOptionNumber As Integer)
'Function to create and send an outlook message with more control than sendobject
'separate multiple recipients or CC, or BCC with comma
'importance - 1=low, 2=normal, 3=high
'AttachmentOptionNumber allows additional Attachment based on which Option
'was clicked in order to send the email. 05-Aug-2003
Dim objOutlook As Outlook.Application
Dim objOutlookMsg As Outlook.MailItem
Dim objOutlookRecip As Outlook.recipient
Dim objOutlookAttach As Outlook.Attachment
Dim txtRecipient As String
Dim stAttachment As String 'Use to store original attachment so that order of attachments for ecp notice will be correct
Dim stattach As String
' 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.
Do While InStr(1, Recipients, ",", vbTextCompare) <> 0 'checks for multiple recipients and adds each
txtRecipient = Left(Recipients, InStr(1, Recipients, ",", vbTextCompare) - 1)
Recipients = Trim(Mid(Recipients, Len(txtRecipient) + 2, Len(Recipients)))
Set objOutlookRecip = .Recipients.Add(txtRecipient)
objOutlookRecip.Type = olTo
Loop
Set objOutlookRecip = .Recipients.Add(Trim(Recipients))
objOutlookRecip.Type = olTo
' Add the CC recipient(s) to the message if existing
If CopyRecipients <> "" Then
Set objOutlookRecip = .Recipients.Add(CopyRecipients)
objOutlookRecip.Type = olCC
End If
' Add the BCC recipient(s) to the message.
If BlindCopyRecipients <> "" Then
Set objOutlookRecip = .Recipients.Add(BlindCopyRecipients)
objOutlookRecip.Type = olBCC
End If
' Set the Subject, Body, and Importance of the message.
.Subject = Subject
.Body = Body & vbCrLf & vbCrLf
Select Case Importance
Case 1
.Importance = olImportanceLow
Case 2
.Importance = olImportanceNormal
Case 3
.Importance = olImportanceHigh
Case Else
.Importance = olImportanceNormal
End Select
' Add attachments to the message.
On Error GoTo SendOutlookMessage_err
If AttachmentPath <> "" Then
If Not IsMissing(AttachmentPath) Then
stAttachment = AttachmentPath
'If AttachmentOptionNumber = 1 Then
' stAttachment = AttachmentPath
' 'change extension to .zip to attach zip rather than pdf
' On Error GoTo ChangeToPDF_err
' AttachmentPath = Replace(AttachmentPath, ".pdf", ".zip")
' On Error GoTo SendOutlookMessage_err
' Set objOutlookAttach = .Attachments.Add(AttachmentPath)
'End If
If AttachmentOptionNumber = 1 Then
'change extension to .zip to attach zip rather than pdf
AttachmentPath = Replace(AttachmentPath, ".pdf", ".zip")
'In order for fIsFileDir to work so that it can check if the file
'exists, needed to store AttachmentPath in a local variable
stattach = AttachmentPath
If fIsFileDIR(stattach) Then
Set objOutlookAttach = .Attachments.Add(AttachmentPath)
Else
'change extension back to .pdf if .zip not found
AttachmentPath = Replace(AttachmentPath, ".zip", ".pdf")
Set objOutlookAttach = .Attachments.Add(AttachmentPath)
End If
On Error GoTo SendOutlookMessage_err
AttachmentPath = stAttachment
End If
' AttachmentPath = stAttachmentpath
Set objOutlookAttach = .Attachments.Add(AttachmentPath)
End If
End If
' Resolve each Recipient's name.
For Each objOutlookRecip In .Recipients
objOutlookRecip.Resolve
Next
' Should we display the message before sending?
If DisplayMsg Then
.Display
Else
.Save
.Send
End If
End With
Set objOutlook = Nothing
Exit Function
SendOutlookMessage_err:
'If the file isn't found, attach the not found pdf file to
'allow code to continue and user to manually attach file to
'the email
AttachmentPath = "M:\Files\NotFound.pdf"
'MsgBox "Old Path " & AttachmentPath
Debug.Print AttachmentPath
'AttachmentPath = "M:\Files\NotFound.pdf"
'MsgBox "New Path " & AttachmentPath
Debug.Print AttachmentPath
Resume
ChangeToPDF_err:
'Change Extension from ZIP to PDF If Attachment isn't found
AttachmentPath = Replace(AttachmentPath, ".zip", ".pdf")
Resume
End Function