gojets1721
Registered User.
- Local time
- Yesterday, 21:50
- 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:
Class Module:
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