Outlook VBA:动态在收件箱和子文件夹上实现ItemChange

时间:2019-03-01 06:32:07

标签: vba outlook-vba

我和this SO question有相同的问题 我还尝试了与答案中建议的方法相同的方法,但仍然无法使它起作用,仅考虑了最后一个文件夹/项目。

基本上,目标是侦听所有文件夹(收件箱和子文件夹)上的所有ItemChange,而无需显式声明每个文件夹和项目的变量。

我想知道您的想法以及我可以采用的任何方法。 让我知道是否可以在不创建加载项的情况下在纯VBA中完成此操作。 我的示例代码如下。

Option Explicit

Private WithEvents FolderItems As Outlook.items
Private WithEvents aaFolder As Outlook.Folder
Private WithEvents aaFolderItems As Outlook.items
Private aaItemsCollection(0 To 2) As Outlook.items
Private aaFolderCollection(0 To 2) As Outlook.Folder


Private data() As String

'Main entry point
Private Sub Application_Startup()

Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")

data = Split("Q1,Q2,Q3", ",") 'hardcoded for testing, will be replaced with for loop to inbox subfolders
Dim index As Integer
For index = 0 To 2
    Set aaFolder = objNS.GetDefaultFolder(olFolderInbox).folders(data(index))
    Set aaFolderCollection(index) = aaFolder
    Set aaFolderItems = aaFolder.items
    Set aaItemsCollection(index) = aaFolderItems
Next
End Sub

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

0 个答案:

没有答案