Mike
Mike

Reputation: 620

Handle Outlook MailItem Send Event in Access

I'm attempting to find a way to catch the MailItem.Send event in Outlook from Access. I've created a class that sets up the objects and that works fine, but the event handler doesn't appear to be doing anything.

Here's the class I created (named OutlookHandler):

Public WithEvents app As Outlook.Application
Public WithEvents msg As Outlook.MailItem


Private Sub Class_Initialize()

    Set app = CreateObject("Outlook.Application")
    Set msg = app.CreateItem(olMailItem)

End Sub


Private Sub msg_Send(Cancel As Boolean)

    MsgBox "Message Sent!"

End Sub

And here is the function where I create an instance of that class:

Public Function test()

    Dim ol As New OutlookHandler

        With ol.msg
            .To = "[email protected]"
            .Subject = "outlook event test"
            .Display
        End With


End Function

When I run test, the email creates and displays. When I hit send in the email, the email sends but the message box never creates. What am I missing?

Upvotes: 2

Views: 1908

Answers (1)

Kazimierz Jawor
Kazimierz Jawor

Reputation: 19067

You need to make your ol variable public in this way:

Dim ol As OutlookHandler
Public Function test()

    'Dim ol  As New OutlookHandler
    Set ol = New OutlookHandler

        With ol.msg
            .To = "[email protected]"
            .Subject = "outlook event test"
            .Display
        End With


End Function

Upvotes: 3

Related Questions