处理Access中的Outlook MailItem发送事件

时间:2014-02-24 20:06:37

标签: vba ms-access outlook

我正试图找到一种方法来从Access中捕获Outlook中的MailItem.Send事件。我已经创建了一个设置对象的类,并且工作正常,但事件处理程序似乎没有做任何事情。

这是我创建的类(名为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

这是我创建该类实例的函数:

Public Function test()

    Dim ol As New OutlookHandler

        With ol.msg
            .To = "mike@anywhere.com"
            .Subject = "outlook event test"
            .Display
        End With


End Function

当我运行test时,电子邮件会创建并显示。当我点击发送电子邮件时,电子邮件会发送但消息框永远不会创建。我错过了什么?

1 个答案:

答案 0 :(得分:2)

您需要以这种方式公开ol variable

Dim ol As OutlookHandler
Public Function test()

    'Dim ol  As New OutlookHandler
    Set ol = New OutlookHandler

        With ol.msg
            .To = "mike@anywhere.com"
            .Subject = "outlook event test"
            .Display
        End With


End Function