Can't get VBA to verify that Outlook message was sent

gojets1721

Registered User.
Local time
Today, 01:27
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
 
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?
 

Users who are viewing this thread

Back
Top Bottom