VBA- from Access use WithEvents on Outlook

Joined
Apr 14, 2009
Messages
1
Reaction score
0
Hi,
I'm trying to create a macro which will send a confirmation from Access using Outlook. I want to make sure that the email is actually sent before updating my database as "email confirmation sent".
I use Office 2003, Win XP and the following VBA code:

In a class module called "clsEmailConfo" here is the following code:

Option Compare Database
Option Explicit

Public WithEvents objOutlook As Outlook.Application
Public objOutlookMsg As Outlook.MailItem


Public Sub Class_Initialize()

Set objOutlook = CreateObject("Outlook.Application")
Set objOutlookMsg = objOutlook.CreateItem(olMailItem)
End Sub

Sub sendEmailConfo(Optional myTo As String, Optional myBcc As String, Optional myCC As String, Optional mySubject As String, Optional myBody As String)
With objOutlookMsg
.To = myTo
.CC = myCC
.BCC = myBcc
'.Attachments.Add (mypathname)
.Subject = "test"

.BodyFormat = olFormatHTML

.HTMLBody = myBody



.Display

End With
End Sub

Private Sub objOutlook_ItemSend(ByVal Item As Object, Cancel As Boolean)

MsgBox ("Mail cancelled")

End Sub



Private Sub objOutlook_NewMail()
MsgBox ("Mail cancelled")
End Sub

Private Sub objOutlook_Quit()
MsgBox ("Mail cancelled")
End Sub


In a normal module, there is the following code which trigger the email:
Option Compare Database
Option Explicit

Sub test()
Dim myEmail As clsEmailConfo
Set myEmail = New clsEmailConfo

myEmail.sendEmailConfo "(e-mail address removed)", , , "test", "test"


End Sub


My problem is that none of the events are being triggered. Any idea why?


Thank you for your help
 
Back
Top