Outlook mail accounts fetch dynamically


I need to fetch the accounts associated in Outlook and use them dynamically.

Example: if a person is having two accounts in outlook,
The username is different for two accounts,

  1. ABC.xyz@company1.com
  2. xyz.abc@company2.com

So now I need username associated to company2.com (xyz.abc)

How to fetch the username??

Bharath Bysani

@bbysani you can have a deep dive into the following VBA Code to get all the Mail accounts listed into Outlook. Ref. https://stackoverflow.com/questions/26519325/how-to-get-the-email-address-of-the-current-logged-in-user

Another Solution would be: Loop over folder “C:\Users<username>\AppData\Local\Microsoft\Outlook” and get the account email address from ost file extension.

Dim OL As Object, olAllUsers As Object, oExchUser As Object, oentry As Object, myitem As Object
Dim User As String

Set OL = CreateObject("outlook.application")
Set olAllUsers = OL.Session.AddressLists.Item("All Users").AddressEntries

User = OL.Session.CurrentUser.Name

Set oentry = olAllUsers.Item(User)

Set oExchUser = oentry.GetExchangeUser()

msgbox oExchUser.PrimarySmtpAddress

once you will get the email address simply use Account property to get the emails.