I'm using the following code to email a PDF report.
If Outlook is already open, the email gets sent immediately upon clicking the 'Send' button in the Outlook message form. It works great!
If Outlook is not already open, Outlook opens and the message form gets filled as expected. However, when I click the 'Send' button Outlook closes and the new message ends up in the Outbox. The new message sits there until I eventually open Outlook, at which time the message gets sent.
I tried adding a DoEvents to keep Outlook open, but that didn't work.
How do I prevent the Outbox delay, or how do I keep Outlook open long enough for the message to leave the Outbox?
I'm using Access/Office 2013.
Thanks,
Sup
If Outlook is already open, the email gets sent immediately upon clicking the 'Send' button in the Outlook message form. It works great!
If Outlook is not already open, Outlook opens and the message form gets filled as expected. However, when I click the 'Send' button Outlook closes and the new message ends up in the Outbox. The new message sits there until I eventually open Outlook, at which time the message gets sent.
I tried adding a DoEvents to keep Outlook open, but that didn't work.
How do I prevent the Outbox delay, or how do I keep Outlook open long enough for the message to leave the Outbox?
I'm using Access/Office 2013.
Thanks,
Sup
Code:
Public Function EmailPDFOutlook(MsgTo As String, _
MsgCC As String, _
MsgBCC As String, _
MsgSubject As String, _
MsgBody As String, _
ReportPath As String)
Dim olApp As Outlook.Application
Dim olMailItem As Outlook.MailItem
On Error GoTo EmailPDFOutlook_Error
' If Outlook is closed, open Outlook
If olApp Is Nothing Then
Set olApp = New Outlook.Application
End If
' Create a new email object
Set olMailItem = olApp.CreateItem(0)
' Add the To/Subject/Body/Attachments to the message then display the message
With olMailItem
.To = MsgTo
.CC = MsgCC
.BCC = MsgBCC
.Subject = MsgSubject
.Body = MsgBody
.Attachments.Add ReportPath
.Display
'.Send
End With
'DoEvents to keep outlook open so mail gets sent?
DoEvents
Set olMailItem = Nothing
Set olApp = Nothing
MsgBox "Email Sent", vbInformation, "Confirm Email"
On Error GoTo 0
Exit Function
EmailPDFOutlook_Error:
MsgBox "Error " & err.Number & " (" & err.Description & ") in procedure EmailPDFOutlook of Module nsEmailPDF"
End Function