首页 > 解决方案 > 有没有办法列出当前 Outlook 会话中可访问的收件箱?

问题描述

我希望在 Outlook 会话中检索电子邮件收件箱列表(即所有可访问的收件箱、主要、共享和委派)。我所需要的只是这些收件箱的字符串电子邮件地址,因此下面的示例代码只是将它们打印在消息框中(当然,这不是它们实际发生的情况!)。

我想我只是很愚蠢,找不到要循环的相关属性?

以下似乎没有返回会话已授权访问的收件箱:

Public Sub PrintAccounts()
    Dim accounts As Outlook.accounts
    Set accounts = Application.Session.accounts
    Dim account As Outlook.account
    For Each account In accounts
        MsgBox account.DisplayName
    Next account
End Sub

非常感谢!

现在回答:

Private Sub DisplayedMailboxesNames()
    Dim colStores As Stores
    Dim oStore As Store
    Set colStores = Session.Stores
    For Each oStore In colStores
        Debug.Print ResolveDisplayNameToSMTP(oStore.displayName)
    Next
End Sub

Private Function ResolveDisplayNameToSMTP(displayName As String) As String
    Dim oRecip As Outlook.Recipient
    Dim oEU As Outlook.ExchangeUser
    Dim oEDL As Outlook.ExchangeDistributionList
    Set oRecip = Application.Session.CreateRecipient(displayName)
    oRecip.Resolve
    If oRecip.Resolved Then
        Select Case oRecip.AddressEntry.AddressEntryUserType
            Case OlAddressEntryUserType.olExchangeUserAddressEntry
            Set oEU = oRecip.AddressEntry.GetExchangeUser
            If Not (oEU Is Nothing) Then
                ResolveDisplayNameToSMTP = oEU.PrimarySmtpAddress
            End If
            Case OlAddressEntryUserType.olExchangeDistributionListAddressEntry
            Set oEDL = oRecip.AddressEntry.GetExchangeDistributionList
            If Not (oEDL Is Nothing) Then
                ResolveDisplayNameToSMTP = oEDL.PrimarySmtpAddress
            End If
        End Select
    End If
End Function

标签: vbaoutlook

解决方案


帐户与邮箱不同。

将所有可访问的邮箱添加到用户的 GUI,然后:

Private Sub DisplayedMailboxesNames()

    Dim colStores As Stores
    Dim oStore As Store
    Dim oRoot As Folder
    
    Set colStores = Session.Stores
    
    For Each oStore In colStores
        Set oRoot = oStore.GetRootFolder
        Debug.Print oRoot.folderPath
        Debug.Print oRoot.Name
    Next
    
End Sub

推荐阅读