mail via Outlook does not send (1 Viewer)

supmktg

Registered User.
Local time
Today, 05:43
Joined
Mar 25, 2002
Messages
360
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
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
 

stepone

Registered User.
Local time
Today, 11:43
Joined
Mar 2, 2004
Messages
97
What happens if you comment out the 'set olApp = Nothing' ?
 

supmktg

Registered User.
Local time
Today, 05:43
Joined
Mar 25, 2002
Messages
360
Outlook still closes. The next time I open Outlook the email is in the Outbox. With 'set olApp = Nothing' commented out, I have to click 'Send all' to send the email. Before, with 'set olApp = Nothing' included, Outlook also closed but the next time I opened Outlook the email was in the Outbox and it automatically sent without having to click anything.

Thanks,
Sup
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:43
Joined
Sep 21, 2011
Messages
14,234
Outlook still closes. The next time I open Outlook the email is in the Outbox. With 'set olApp = Nothing' commented out, I have to click 'Send all' to send the email. Before, with 'set olApp = Nothing' included, Outlook also closed but the next time I opened Outlook the email was in the Outbox and it automatically sent without having to click anything.

Thanks,
Sup

I also have this in my code, but have just realised I never set it, so always false)
Code:
' Should we display the message before sending?
            If blnDisplayMsg Then
                .Display
            Else
                .Save
                .Send
            End If
I now notice you have commented out .Send ?
 

supmktg

Registered User.
Local time
Today, 05:43
Joined
Mar 25, 2002
Messages
360
Yes, I commented out ".send" because I want to display the email in order to edit the message and then manually click the Outlook send button. With the ".send" included, Outlook automatically sends the email before I have a chance to edit it. I believe .send and .display are meant to be either/or, not at the same time.
 

GinaWhipp

AWF VIP
Local time
Today, 06:43
Joined
Jun 21, 2011
Messages
5,900
Hmm, you might need...

Code:
Set olApp = CreateObject("Outlook.Application")
 

Gasman

Enthusiastic Amateur
Local time
Today, 11:43
Joined
Sep 21, 2011
Messages
14,234
Hmm,

I *thought* I had posted a lot of code before I posted the message above, as it does not make sense with out the code.?

I do sometimes however get error 429 Activex cannot create the object, which is why I use the Pause function, but have also tried DoEvents with same result.

Code behind the button click event

Code:
    ' See if Outlook is open, otherwise open it
    If fIsOutlookRunning = False Then
        Call OpenOutlook
        Pause (5)
    End If
    
    
    ' Create the Outlook session.
    Set objOutlook = GetObject(, "Outlook.Application")


Code:
Public Sub OpenOutlook()

    'Depending on your version of Access, Access.hWndAccessApp could also be Application.hWnd
    If ShellExecute(Access.hWndAccessApp, vbNullString, "Outlook", vbNullString, "C:\", 1) < 33 Then
        MsgBox "Outlook not found."
    End If
End Sub

Code:
Option Compare Database
Option Explicit
Public Declare Function ShellExecute Lib "shell32.dll" Alias "ShellExecuteA" _
    (ByVal hWnd As Long, ByVal lpOperation As String, ByVal lpFile As String, _
    ByVal lpParameters As String, ByVal lpDirectory As String, _
    ByVal nShowCmd As Long) As Long

Public Function fIsOutlookRunning() As Boolean
Dim W As Object
Dim processes As Object
Dim process As Object

fIsOutlookRunning = False

Set W = GetObject("winmgmts:")
Set processes = W.execquery("SELECT * FROM win32_process")

For Each process In processes
    If process.Name = "OUTLOOK.EXE" Then
        
        fIsOutlookRunning = True
        Exit For
        
    End If
Next

Set W = Nothing
Set processes = Nothing
Set process = Nothing

End Function

Public Function Pause(NumberOfSeconds As Variant)
On Error GoTo Err_Pause

    Dim PauseTime As Variant, start As Variant

    PauseTime = NumberOfSeconds
    start = Timer
    Do While Timer < start + PauseTime
    DoEvents
    Loop

Exit_Pause:
    Exit Function

Err_Pause:
    MsgBox Err.Number & " - " & Err.Description, vbCritical, "Pause()"
    Resume Exit_Pause

End Function
 

supmktg

Registered User.
Local time
Today, 05:43
Joined
Mar 25, 2002
Messages
360
In the end I discovered the problem was caused by the fact that the file I am attaching is on a mapped drive on an online Sharepoint server, which takes forever to attach. When I attach a temp copy of the file from a local folder the issue goes away.

Sorry this took so long to figure out and respond. Thank you everyone for your help!

Sup
 

GinaWhipp

AWF VIP
Local time
Today, 06:43
Joined
Jun 21, 2011
Messages
5,900
Nope... thank you for telling us what happened! :D
 

Users who are viewing this thread

Top Bottom