I send email using Excel VBA. I want to record whether the message has been sent.
I have taken some code from another post.
I created the class as described, and put in a few extra bits to see if it is working.
It initializes, but then nothing else happens. After the mail is sent, the class remains open in the background somehow, so I have to stop it in the VBE.
Here is the calling code:
Sub SendProc2(add As String)
Set OutApp = CreateObject("Outlook.Application")
Set OutMail = OutApp.CreateItem(0)
On Error Resume Next
With OutMail
.To = add
.CC = ""
.BCC = ""
.Subject = ThisWorkbook.Name
.Body = Application.WorksheetFunction.VLookup(Worksheets("Data").Range("B135"), Range("formversion"), 2, False) _
& " Attached:" & vbCrLf & vbCrLf & ThisWorkbook.Name
.Attachments.add ActiveWorkbook.FullName
.Display 'or use .Send
End With
Dim CurrWatcher As EmailWatcher
Set CurrWatcher = New EmailWatcher
Set CurrWatcher.TheMail = OutMail
On Error GoTo 0
Set OutMail = Nothing
Set OutApp = Nothing
Unload UserForm4
End Sub
The Class Module Code which is called EmailWatcher:
Option Explicit
Public WithEvents TheMail As Outlook.MailItem
Private Sub Class_Terminate()
Debug.Print "Terminate " & Now()
End Sub
Private Sub TheMail_Send(Cancel As Boolean)
Debug.Print "Send " & Now()
'enter code here
End Sub
Private Sub Class_Initialize()
Debug.Print "Initialize " & Now()
End Sub
It never seems to register the _Send
, which I think might be something to do with the class object not being defined or something else. Sometimes I get warnings, at the moment it is initializing, then terminating immediately without waiting for the _Send
.
Using Excel 2007, on Windows 7, over a local authority network that I have no control over.