如何为许多Outlook子文件夹实施ItemChange? [英] How to implement ItemChange for many Outlook subfolders?

查看:84
本文介绍了如何为许多Outlook子文件夹实施ItemChange?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Outlook中的VBA模块中,我目前具有以下代码:

In a VBA module in Outlook I have currently code like this:

Private WithEvents AAInboxItems As Outlook.Items
Private WithEvents AASentItems As Outlook.Items
Private WithEvents AADoneItems As Outlook.Items

Private Sub AAInboxItems_ItemChange(ByVal Item As Object)
    'Do Something
End Sub
Private Sub AASentItems_ItemChange(ByVal Item As Object)
    'Do Something
End Sub
Private Sub AADoneItems_ItemChange(ByVal Item As Object)
    'Do Something
End Sub

以上并不是完整的代码,只是为了说明原理.对于我实现了此功能的几个文件夹,此方法效果很好.

Above is not the complete code, just to show the principle. This works fine for a couple of folders for which I implemented this.

我希望收件箱的所有子文件夹都有此类事件.这应该是动态的.如果用户创建一个新的子文件夹,那么我不想更改代码. 我希望有一个事件在任何Outlook收件箱子文件夹中更改项目时触发.

I would like to have such events for all subfolders of the Inbox. And this should work dynamically. If the user creates a new sub-folder then I don't want to change the code. I want to have an event which fires when an item is changed in any Outlook Inbox subfolder.

可以吗?怎么样?

编辑:使用Dmitry Streblechenko的答案,我尝试了以下操作,但它没有完成我想要的操作-可能是我实施不正确. 该事件仅针对最后分配的文件夹而非所有文件夹触发. 这是我的预期,但也许我做错了或不正确的答案.我将这些信息放在问题中,因为它不适合对Dmitry的答案发表评论.

With Dmitry Streblechenko's answer I tried the following but it does not do what I want it to do - maybe I implemented it incorrectly. The events fire but only for the last assigned folder and not all folders. This is what I expected but maybe I made something wrong or didn't understand the answer correct. I put this information in the question because it won't fit in a comment to Dmitry's answer.

以下是代码中最重要的部分.我省略了很多细节以使其更短.基本上可以,但是仅适用于一个文件夹.

The following are the most important parts of the code. I leave lots of details out to make it shorter. Basically it works, but only for one folder.

Option Explicit
Global gbl_FolderItems(3) As Outlook.Items
Private WithEvents FolderItems As Outlook.Items

Private Sub Application_Startup()
    For intI = 1 To 3
        'This works only with the last folder
        'Set gbl_FolderItems(intI) = objGetFolderItems("Folder" & intI)
        'Set FolderItems = gbl_FolderItems(intI)

        'This works only with the last folder
        Set FolderItems = objGetFolderItems("Folder" & intI)
        Set gbl_FolderItems(intI) = FolderItems
    Next
End Sub

Private Function objGetFolderItems(strFolderShortName As String) As Outlook.Items
    Dim olApp As Outlook.Application
    Set olApp = Outlook.Application
    Dim objNS As Outlook.NameSpace
    Set objNS = olApp.GetNamespace("MAPI")
    Dim obj As Outlook.Items

    Select Case strFolderShortName
    Case "Folder1"
        Set obj = objNS.Folders("MyAccount").Folders("Inbox").Folders("Folder1").Items
    Case "Folder2"
        Set obj = objNS.Folders("MyAccount").Folders("Inbox").Folders("Folder2").Items
    Case "Folder3"
        Set obj = objNS.Folders("MyAccount").Folders("Inbox").Folders("Folder1").Folders("Folder3").Items
    End Select
    Set objGetFolderItems = obj
End Function

Private Sub FolderItems_ItemChange(ByVal Item As Object)
    Debug.Print "FolderItems_ItemChange(" & Item.Subject & ")"
End Sub

Private Sub FolderItems_ItemAdd(ByVal Item As Object)
    Debug.Print "FolderItems_ItemAdd(" & Item.Subject & ")"
End Sub

推荐答案

声明单个WithEvents Items变量,循环浏览要跟踪的文件夹,分配Items变量,并将其存储在全局数组中.即使变量在每次迭代中都会被覆盖,但由于所有不同的Items对象仍处于活动状态,并且由于数组引用了它们而引发了事件,因此将监视所有文件夹.

Declare a single WithEvents Items variable, loop through the folders that you want to track, assign the Items variable, and store it in a global array. Even though the variable will be overwritten on each iteration, all of the folders will be monitored because all the different Items objects are still alive and raising events since they are referenced by the array.

这篇关于如何为许多Outlook子文件夹实施ItemChange?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆