Public Function SendEmailUsingOutlook(strSendTo As String, strCopyTo As String, strSubject As String, strMessage As String, strAttachment As String)
On Error GoTo ErrHandler
'CR v5161 - Uses late binding to send email without displaying Outlook
Dim objOutlook As Object
Dim objOutlookMsg As Object
Dim StartOutlookFlag As Boolean
StartOutlookFlag = False
' Create the Outlook session.
If IsAppRunning("Outlook.Application") = True Then
'Use existing instance of Outlook
Set objOutlook = CreateObject("Outlook.Application")
Else
'Could not get instance of Outlook, so create a new one
Path = GetAppExePath("outlook.exe") 'determine outlook's installation path
Shell (Path), vbMinimizedFocus 'start outlook
Do While Not IsAppRunning("Outlook.Application")
DoEvents
Loop
Set objOutlook = GetObject(, "Outlook.Application") 'Bind to new instance of Outlook
StartOutlookFlag = True 'needed so Outlook can be closed later
End If
' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(0)
' Add the To/Subject/Body/Attachments to the message then send the message
With objOutlookMsg
.To = strSendTo
.CC = strCopyTo
.Subject = strSubject
.Body = strMessage
If Nz(strAttachment, "") <> "" Then
.Attachments.Add strAttachment
End If
' .Display 'do not display message
.Save
.Send
End With
Set objOutlook = Nothing
Set objOutlookMsg = Nothing
'close Outlook if it was opened for this function - time may need modifiying
DoEvents
Wait 5 'allow time to send message
If StartOutlookFlag = True Then CloseOutlook
ErrHandlerExit:
Exit Function
ErrHandler:
If Err.Number <> 287 Then 'And err.Number <> 429 Then
MsgBox "Error " & Err.Number & " in SendEMailUsingOutlook routine: " & Err.Description
End If
Resume ErrHandlerExit
End Function
Public Function SendEmailDisplayOutlook(strSendTo As String, strCopyTo As String, strSubject As String, strMessage As String, strAttachment As String)
On Error GoTo ErrHandler
'Uses late binding to send email(so that a reference to Outlook library is not needed)
Dim objOutlook As Object
Dim objOutlookMsg As Object
Dim sAPPPath As String
' Create the Outlook session.
Set objOutlook = CreateObject("Outlook.Application")
' Create the message.
Set objOutlookMsg = objOutlook.CreateItem(0)
' Add the To/Subject/Body/Attachments to the message then display the message for editing
With objOutlookMsg
.To = strSendTo
.CC = strCopyTo
.Subject = strSubject
.Body = strMessage
If Nz(strAttachment, "") <> "" Then
.Attachments.Add strAttachment
End If
.display
'.Send
End With
Set objOutlook = Nothing
Set objOutlookMsg = Nothing
ErrHandlerExit:
Exit Function
ErrHandler:
MsgBox "Error " & Err.Number & " in SendEMailDisplayOutlook routine: " & Err.Description
Resume ErrHandlerExit
End Function