Getting Alias from items in Outlook AddressBook with VBA

213 Views Asked by At

My code below gives me the following error at the Debug.Print(oExuser.Alias), why?

enter image description here

Sub Test()

Dim AliasName, FullName As String
Dim outlookApp As Outlook.Application
Dim myNameSpace As Outlook.nameSpace
Dim myAddrList As AddressList
Dim myAddrEntries As AddressEntries
Dim myAddrEntry As Outlook.AddressEntry
Dim myAlias As Object
Dim oExUser As Outlook.ExchangeUser

Set outlookApp = New Outlook.Application
Set myNameSpace = outlookApp.GetNamespace("MAPI")
Set myAddrList = myNameSpace.GetGlobalAddressList()


Set myAddrEntries = myAddrList.AddressEntries
Set myAddrEntry = myAddrEntries.Item(1)

Set oExUser = myAddrEntry.GetExchangeUser

Debug.Print (oExUser.Alias)

End Sub
2

There are 2 best solutions below

0
Eugene Astafiev On

You have to be connected to the Exchange server to use the AddressEntry.GetExchangeUser method. The following code makes sense only for the Exchange accounts:

Set oExUser = myAddrEntry.GetExchangeUser

Debug.Print (oExUser.Alias)

The Alias property returns an empty string if this property has not been implemented or does not exist for the ExchangeUser object.

0
Dmitry Streblechenko On

You need to check that the returned ExchangeUser object (oExUser) is not null. It will be null for the non-Exchange (e.g., SMTP) address entries even if you have Exchange in the current Outlook profile:

If not (oExUser Is Nothing) Then
    Debug.Print (oExUser.Alias)
End If