Accounts and Account Types in Outlook Add-in using VBA code example

Accounts and Account Types in Outlook Add-in using VBA code example

Account

Object refers to the account which is configured in current profile of Outlook. Outlook object offers Accounts collection which holds various properties and methods to manipulate an account. In this example we will iterate through each account and use following properties to obtain necessary information from an account:

Properties

  • DisplayName: refers to name of the account attached to current profile
  • UserName: refers to the user associated to the account
  • SmtpAddress: refers to SMTP Address of the account
  • AccountType: offers various constants to validate account type as listed below:
    • olExchange
    • olHttp
    • olImap
    • olOtherAccount
    • olPop3

Code example

Public Sub GetAccounts()
'Outlook application object declaration
Dim oApp As Outlook.Application

'bind current outlook instance
Set oApp = GetObject("", "Outlook.Application")
    
Dim oAccounts As Outlook.Accounts
'Get list of available accounts in outlook
Set oAccounts = oApp.Session.Accounts

Dim oAcc As Outlook.Account

'Iterate each account
For Each oAcc In oAccounts
    Debug.Print "Account Name : " & oAcc.DisplayName
    Debug.Print "Username : " & oAcc.UserName
    Debug.Print "SMTP Addres: " & oAcc.SmtpAddress
    
    'Get account type
    Select Case oAcc.AccountType
        Case olExchange:
            Debug.Print "Its an Exchange Account"
        Case olHttp:
            Debug.Print "Its a HTTP Account"
        Case olImap:
            Debug.Print "Its an Imap Account"
        Case olOtherAccount:
            Debug.Print "Its an Other Account"
        Case olPop3:
            Debug.Print "Its a POP3 Account"
    End Select
Next oAcc

'CleanUp
If Not oAcc Is Nothing Then
    Set oAcc = Nothing
End If
If Not oAccounts Is Nothing Then
    Set oAccounts = Nothing
End If
If Not oApp Is Nothing Then
    Set oApp = Nothing
End If
End Sub

Output

Next >> Save Outlook Attachments in Local Drive using VBA

Leave a Reply

Your email address will not be published.