Can't get VBA to verify that Outlook message was sent (1 Viewer)

gojets1721

Registered User.
Local time
Today, 01:25
Joined
Jun 11, 2019
Messages
430
So I have the below code in a command to send an Outlook email via VBA. It works great but I want to have it test if the email was actually sent (since I'm using '.display' and not '.send'). I want to update a field if the email is sent.

I used this guide but I can't seem to figure out what I'm doing wrong. In the class module, I used msgboxes to test if its working and regardless of whether I send the email or not, neither pop up.

Basically, all I want is for Me.Status to update to 'Completed' if the email is sent, and do nothing if the email is not sent

Command button in form:
Code:
Private Sub btnEmailEvent_Click()
On Error GoTo btnEmailEvent_Click_Err
    
    Dim O As Outlook.Application
    Dim M As Outlook.MailItem
    Dim itmevt As New CMailItemEvents
    
    Set O = New Outlook.Application
    Set M = O.CreateItem(olMailItem)
    Set itmevt.itm = M
    
    With M
        .BodyFormat = olFormatHTML
        .HTMLBody = "
        .To = ""
        .CC = ""
        .BCC = ""
        .Subject = "Emailed Report"
       .Display
    End With
    
    Set M = Nothing
    Set O = Nothing

btnEmailEvent_Click_Exit:
    Exit Sub

btnEmailEvent_Click_Err:

    MsgBox "Error #" & Err.Number & " - " & Err.Description, , "Error"
    
    End If
    
End Sub

Class Module:
Code:
Option Explicit
Public WithEvents itm As Outlook.MailItem
Private Sub itm_Close(Cancel As Boolean)
   Dim blnSent As Boolean
   On Error Resume Next
   blnSent = itm.Sent
   If Err.Number = 0 Then
      MsgBox "Email not sent"
   Else
      MsgBox "Email sent"
   End If
End Sub
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 04:25
Joined
Feb 19, 2002
Messages
43,275
Have you tried looking in the sent folder? I'm not sure when an email gets moved to sent so you might need to do a loop using a timer and try for at least a minute. That also means you need to tell the user what you are doing so he doesn't think the code is locked up.
 

gojets1721

Registered User.
Local time
Today, 01:25
Joined
Jun 11, 2019
Messages
430
Have you tried looking in the sent folder? I'm not sure when an email gets moved to sent so you might need to do a loop using a timer and try for at least a minute. That also means you need to tell the user what you are doing so he doesn't think the code is locked up.
Gotcha. I haven't tried that. How would you suggest that that's coded? Any guides you know of?
 

Pat Hartman

Super Moderator
Staff member
Local time
Today, 04:25
Joined
Feb 19, 2002
Messages
43,275
I don't have any code that does this. I would start by linking to the sent folder and go from there. There needs to be something unique so you can figure out what message you are looking for. I have not monitored this process. I'm guessing the message goes first into the Outbox and then when Outlook cycles, it gets sent and then moved to the Sent folder. So, the Outbox should only ever contain messages waiting to be sent. You might be able to use the Normalized Subject field.

Also, my linked tables operate very slowly so I would recommend that you use a special email address, not your own, to send these messages and clean out the sent folder frequently so it doesn't end up with thousands of old messages in it.

When I send emails, I send them via code. I don't open them and leave it to the user to send which is why I've never had to figure out if the user actually sent the message.
 

Users who are viewing this thread

Top Bottom