利用Excel中的Outlook事件 [英] Utilizing Outlook Events From Excel
问题描述
我试图利用ItemAdd事件 MSDN Link Here 从Outlook宏中的Outlook对象模型;但是,我未能找到一种方法来利用Excel外的事件。
I'm attempting to utilize the "ItemAdd" Event MSDN Link Here from the Outlook Object Model in a Excel macro; however, I haven't been able to find a way to utilize events from outside of Excel.
我的预期用例是监视共享邮箱和日志中收到的项目他们的接待。我目前正在一个计时器上运行一个循环来检查新的电子邮件,但是如果可能的话,我宁愿在每次事件触发时简单地记录。
My intended use case is to monitor items received in a shared mailbox and log their reception. I am currently running a loop on a timer that checks for new emails, however I would prefer to simply log each time an event fires if possible.
不幸的是,我无法直接从Outlook运行任何宏,所以我只能从其他Office应用程序访问Outlook对象模型。
Unfortunately I am not able to run any macros from Outlook directly so I am restricted to accessing the Outlook Object Model from other Office applications.
推荐答案
你可以这样做,但你需要在Excel中使用一个Class模块来完成它。
You can do this, but you need to use a Class module in Excel to accomplish it.
类模块 - myOutlook,或者称之为任何你想要的。
Class Module - "myOutlook" or call it whatever you want.
Private WithEvents myItems As Outlook.Items
Private Sub Class_Initialize()
Dim oNS As Namespace
Dim myOL As Outlook.Application
Set myOL = New Outlook.Application
Set oNS = myOL.GetNamespace("MAPI")
Set myItems = oNS.GetDefaultFolder(olFolderInbox).Items
'Set this equal to the folder you wish to use this on
End Sub
Private Sub myItems_ItemAdd(ByVal Item As Object)
Debug.Print "Got_EMAIL!!!"
End Sub
然后,在常规模块中执行以下操作: p>
Then, in a regular module do this:
Dim myOutlook As myOutlook
Sub TestSub()
Set myOutlook = New myOutlook
End Sub
初始化用户定义类的实例,这些事件将被它捕获。
Once you initialize the instance of your user defined class, the events will be caught by it.
显然,您将需要将myItems对象设置为链接到正确的收件箱。对于我来说,它只是链接到我的默认邮箱,这是最简单的测试。
Obviously, you will need to set the "myItems" object to be linked to the correct inbox. For mine, it's just linked to my default mailbox which was easiest for testing.
这篇关于利用Excel中的Outlook事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!