'VBA Outlook: How to get the smtp address of the selected folder account

I have different accounts in my Outlook. Depending on the currently selected folder, I would like to find the smtp Email address of the corresponding account. (Folder name is no help)

I know how to get the smtp email address of an account: (olApp.Session.CurrentUser.AddressEntry.GetExchangeUser.PrimarySmtpAddress)

I know how to get the current selected folder or even its store name: (olApp.ActiveExplorer.CurrentFolder.store.DisplayName)

but I can't find how to link both information...

Any idea ?

thx :)



Solution 1:[1]

Private Sub storeAddress_from_DisplayName()

Dim storeDisplayName As String
Dim storeSMTPAddress As String

Dim storeRecipient As Recipient

' DisplayName and PrimarySmtpAddress can be the same

storeDisplayName = ActiveExplorer.CurrentFolder.Store.DisplayName
Debug.Print " storeDisplayName: " & storeDisplayName

Set storeRecipient = Session.CreateRecipient(storeDisplayName)

If storeRecipient.AddressEntry.Type = "EX" Then
    storeSMTPAddress = storeRecipient.AddressEntry.GetExchangeUser.PrimarySmtpAddress
    Debug.Print " storeSMTPAddress: " & storeSMTPAddress
End If

End Sub

Solution 2:[2]

In theory, you could parse the EX store entry id to extract the EX address and then use it to build the GAL entry id that you can use to call Namespace.GetAddressEntryFromID. You can see how the store entry is parsed (in C++) in the MFCMAPI source code.

If using Redemption (I am its author) is an option, it exposes RDOExchangeMailboxStore.Owner property (returns RDOAddressEntry object, which in turn exposes the SMTPAddress property):

Set MySession = CreateObject("Redemption.RDOSession")
MySession.MAPIOBJECT = Application.Session.MAPIOBJECT
set Store = MySession.GetStoreFromID(Application.ActiveExplorer.CurrentFolder.StoreID)
MsgBox Store.Owner.SmtpAddress

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 niton
Solution 2