Excel/Outlook VBA - select 个不同邮箱中的文件夹
Excel/Outlook VBA - select folders in different Mailboxes
我想使用 Excel VBA 从 Outlook 收集一些电子邮件计数数据。除了个人邮箱,我还可以访问其他邮箱account/stores(?)。 Outlook mailbox/accounts 上的标签是 HR
、Marketing
、Accounting
。我想遍历这些 mailboxes/stores 中的文件夹并执行一些任务。
当我通过 Sessions.Accounts
编写 for 循环时,它只访问我的默认邮箱(即不访问 HR
、Marketing
、Accounting
)。如果我循环访问 Session.Stores
:
,我似乎可以访问我的非默认邮箱
Sub test()
Dim olApp As Outlook.Application
Set olApp = Outlook.Application
For Each acct In olApp.Session.Stores
MsgBox acct
'how can I access folders and emails within these Stores?
End Sub
此 returns 个消息框带有 HR
、Marketing
、Accounting
。不过,我无法 select 这些非默认邮箱中的收件箱或其他文件夹。
对于我的默认邮箱,我能够得到它:
Sub default_email_count()
Dim olApp As Outlook.Application
Dim objNS As Outlook.Namespace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set olFolder = olbNS.GetDefaultFolder(olFolderInbox)
Set olFolder = olFolder.Folders("sample_folder")
Count = olFolder.Items.Count
Msgbox Count
'this code successfully outputs the count of emails in my default mailbox within the folder "sample folder"
如何扩展上述代码以在其他 Stores
而不仅仅是我的默认邮箱中工作?
提前致谢。
如果邮箱已经在您的个人资料中,请使用 Namespace.Stores
集合。可以使用 Store.GetDefaultFolder
(而不是 Namespace.GetDefaultFolder
)访问默认文件夹。
您也可以使用 Namespace.GetSharedDefaultFolder
- 它以 Recipient
对象作为其参数之一(您可以从 Namespace.CreateRecipient
获取)。
以下代码显示了如何为 Outlook 会话枚举所有商店中的所有文件夹。
代码示例首先使用当前会话的 NameSpace.Stores 属性 获取当前会话的所有商店,Application.Session
.
对于本次会话的每个商店,它使用Store.GetRootFolder获取商店根目录下的文件夹。
对于每个商店的根文件夹,它迭代地调用 EnumerateFolders
过程,直到它访问并显示该树中每个文件夹的名称。
Sub EnumerateFoldersInStores()
Dim colStores As Outlook.Stores
Dim oStore As Outlook.Store
Dim oRoot As Outlook.Folder
On Error Resume Next
Set colStores = Application.Session.Stores
For Each oStore In colStores
Set oRoot = oStore.GetRootFolder
Debug.Print (oRoot.FolderPath)
EnumerateFolders oRoot
Next
End Sub
Private Sub EnumerateFolders(ByVal oFolder As Outlook.Folder)
Dim folders As Outlook.folders
Dim Folder As Outlook.Folder
Dim foldercount As Integer
On Error Resume Next
Set folders = oFolder.folders
foldercount = folders.Count
'Check if there are any folders below oFolder
If foldercount Then
For Each Folder In folders
Debug.Print (Folder.FolderPath)
EnumerateFolders Folder
Next
End If
End Sub
我想使用 Excel VBA 从 Outlook 收集一些电子邮件计数数据。除了个人邮箱,我还可以访问其他邮箱account/stores(?)。 Outlook mailbox/accounts 上的标签是 HR
、Marketing
、Accounting
。我想遍历这些 mailboxes/stores 中的文件夹并执行一些任务。
当我通过 Sessions.Accounts
编写 for 循环时,它只访问我的默认邮箱(即不访问 HR
、Marketing
、Accounting
)。如果我循环访问 Session.Stores
:
Sub test()
Dim olApp As Outlook.Application
Set olApp = Outlook.Application
For Each acct In olApp.Session.Stores
MsgBox acct
'how can I access folders and emails within these Stores?
End Sub
此 returns 个消息框带有 HR
、Marketing
、Accounting
。不过,我无法 select 这些非默认邮箱中的收件箱或其他文件夹。
对于我的默认邮箱,我能够得到它:
Sub default_email_count()
Dim olApp As Outlook.Application
Dim objNS As Outlook.Namespace
Set olApp = Outlook.Application
Set objNS = olApp.GetNamespace("MAPI")
Set olFolder = olbNS.GetDefaultFolder(olFolderInbox)
Set olFolder = olFolder.Folders("sample_folder")
Count = olFolder.Items.Count
Msgbox Count
'this code successfully outputs the count of emails in my default mailbox within the folder "sample folder"
如何扩展上述代码以在其他 Stores
而不仅仅是我的默认邮箱中工作?
提前致谢。
如果邮箱已经在您的个人资料中,请使用 Namespace.Stores
集合。可以使用 Store.GetDefaultFolder
(而不是 Namespace.GetDefaultFolder
)访问默认文件夹。
您也可以使用 Namespace.GetSharedDefaultFolder
- 它以 Recipient
对象作为其参数之一(您可以从 Namespace.CreateRecipient
获取)。
以下代码显示了如何为 Outlook 会话枚举所有商店中的所有文件夹。
代码示例首先使用当前会话的 NameSpace.Stores 属性 获取当前会话的所有商店,
Application.Session
.对于本次会话的每个商店,它使用Store.GetRootFolder获取商店根目录下的文件夹。
对于每个商店的根文件夹,它迭代地调用
EnumerateFolders
过程,直到它访问并显示该树中每个文件夹的名称。
Sub EnumerateFoldersInStores()
Dim colStores As Outlook.Stores
Dim oStore As Outlook.Store
Dim oRoot As Outlook.Folder
On Error Resume Next
Set colStores = Application.Session.Stores
For Each oStore In colStores
Set oRoot = oStore.GetRootFolder
Debug.Print (oRoot.FolderPath)
EnumerateFolders oRoot
Next
End Sub
Private Sub EnumerateFolders(ByVal oFolder As Outlook.Folder)
Dim folders As Outlook.folders
Dim Folder As Outlook.Folder
Dim foldercount As Integer
On Error Resume Next
Set folders = oFolder.folders
foldercount = folders.Count
'Check if there are any folders below oFolder
If foldercount Then
For Each Folder In folders
Debug.Print (Folder.FolderPath)
EnumerateFolders Folder
Next
End If
End Sub