简介:我想按“新电子邮件”,然后监视结果电子邮件中的事件,特别是附件的添加。
我正在MS Outlook下的VBA中工作。我希望监视新起草的电子邮件中是否有任何添加的附件。我的目标是在添加附件时使用AttachmentAdd事件在电子邮件上触发更新。 我想我需要做的是知道何时创建了“新电子邮件”,我可以开始收听事件了。我通过初始化其他文件夹“ WithEvents”来实现此目的。因此,我想我希望对产生“新电子邮件”的文件夹/集合执行相同的操作。我该如何设置这样的Listen?在Outlook中的集合层次结构中的哪里创建了“新电子邮件”?我不知道放置初始钩的位置。
以下是我对我知道的文件夹执行的操作的示例:
Private WithEvents olDeletedItems As Items
'Initialize system to establish locations to monitor
Private Sub Application_Startup()
Dim objNS As NameSpace
Dim objFolder As Outlook.Folder
Set objNS = Application.Session
'Instantiate objects declared WithEvents
Set objFolder = objNS.Folders("me@email.com").Folders("Deleted Items")
Set olDeletedItems = objFolder.Items
End Sub
'Actions on Deleted Items
'Marked deleted items as read as they are deleted
Private Sub olDeletedItems_ItemAdd(ByVal x As Object)
x.UnRead = False
x.Save
End Sub
答案 0 :(得分:0)
似乎可以通过驱动“检查员”来获得结果。查找何时创建了新的“检查器”,然后使用其拉出当前的“ MailItem”。然后可以监视此“ MailItem”的事件。
'Monitor for Inspector Events
Private WithEvents olInspectors As Inspectors
'Delcare a place to work with a MailItem that is monitored
Private WithEvents olTempMail As MailItem
'I use this to instantiate all my monitors
Private Sub Application_Startup()
Set olInspectors = Application.Inspectors 'Initialize
End Sub
'Here we look for a new Inspector to be created,
'then pull the item from the inpsector and push it
'to the olTempMail var that we can watch it for events
Private Sub olInspectors_NewInspector(ByVal x As Inspector)
Set olTempMail = x.CurrentItem
End Sub
'Here is a sample event watch where we respond to an
'attachment being added to the MailItem pulled from
'the inspector above.
Private Sub olTempMail_attachmentadd(ByVal x As Attachment)
Debug.Print x.DisplayName
End Sub
在应用程序中,这将需要添加一些过滤和条件以使其正常运行。但是,这应该使事情朝着能够对为在MS Outlook中按下“新建电子邮件”按钮时创建的新MailItem创建的事件做出反应做出反应。