简而言之:我希望按“新电子邮件”,然后监控生成的事件电子邮件,特别是添加附件。
我在MS Outlook下的VBA工作。我希望监控新起草的电子邮件以查找任何添加的附件。我的目标是在添加附件时使用AttachmentAdd事件触发电子邮件的更新。我认为我需要做的是知道什么时候创建了“新电子邮件”,我可以开始倾听这个事件。我通过初始化'WithEvents'来对其他文件夹执行此操作。因此,我想我希望对产生“新电子邮件”的文件夹/集合做同样的事情。我怎么能设置这样的听?在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("[email protected]").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
似乎可以通过驾驶'检查员'来获得结果。查找创建新“检查器”的时间,然后使用它来拉取当前的“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创建的事件做出反应。