Playing with Outlook AddressLists

Playing with the address book isn't my idea of a good time, but here are some code snippets that might make your day.

You might work in an office that uses Exchange, if so, then you might have several address books from which to select contacts. The problem is, which address list is the contact located in?

The following code will loop through every address list and tries to set an object reference to the relevant AddressEntry Object. If found, the loop stops and the object is returned.

Function GetGALEntry(name As String) As Outlook.AddressEntry

  Dim olAL As Outlook.AddressList
  Dim olALs As Outlook.AddressLists
  Dim currentAL As Outlook.AddressList

  Set olALs = GetNS(GetOutlookApp).AddressLists

  ' look for contact in each AddressList
  For Each olAL In olALs

    Set currentAL = olAL

    ' if the name isn't found, an error occurs
    On Error Resume Next
    Set GetGALEntry = currentAL.AddressEntries(name)

    If Not GetGALEntry Is Nothing Then
      Exit Function
    End If
  Next olAL

End Function

Function GetOutlookApp() As Outlook.Application
' returns native Outlook.Application object
  Set GetOutlookApp = Outlook.Application
End Function

Function GetNS(ByRef app As Outlook.Application) As Outlook.NameSpace
' returns native NameSpace Object
  Set GetNS = app.GetNamespace("MAPI")
End Function

Sample usage

This is how we iterate through the AddressEntry object. I've added some inline comments so you can see what values are returned by each property.

Sub Test_GAL_Loop()

  On Error GoTo ErrorHandler

  Dim olEntry As Outlook.AddressEntry
  Dim olEntryParent As Outlook.AddressList

  Set olEntry = GetGALEntry("Pena, Jimmy")

  ' DisplayType:
  'Constant            Value
  'olAgent               - 3
  'olDistList             - 1
  'olForum               - 2
  'olOrganization     - 4
  'olPrivateDistList  - 5
  'olRemoteUser      - 6
  'olUser                  - 0
  Debug.Print olEntry.DisplayType

  ' Type = "EX" for Exchange users, "SMTP" for an Internet-style address
  Debug.Print olEntry.Type

  ' the email address (see Type property for more info)
  Debug.Print olEntry.Address

  ' the address list it was found on
  Debug.Print olEntry.Parent.Parent

ProgramExit:
  Exit Sub
ErrorHandler:
  MsgBox Err.Number & " - " & Err.Description
  Resume ProgramExit
End Sub

Year End Recap

This is the last post for this blog in 2009. The next post will be the first one in 2010. Now is the time to decide if this was a good or bad year. I hope you all enjoyed this year as much as I did. 2009 saw about 160 blog posts, nearly 80% of the total blog comments, and more hits than I can count.

Next year should see even more, as I move forward to meet my goals for this site:

  • Greater visibility in the VB/Office community
  • Possibly a new WordPress theme
  • At least 100 site pages (currently 65)
  • Guest posts
  • Bigger contests and giveaways
  • Article syndication

Stay tuned, it only gets better!

Related Articles:

About JP

I'm just an average guy who writes VBA code for a living. This is my personal blog. Excel and Outlook are my thing, with a sprinkle of Access and Word here and there. Follow this space to learn more about VBA. Keep Reading »

Share This Article:

Share and bookmark this articledelicious buttonfacebook buttonlinkedin buttonstumbleupon buttontwitter button
Comments on this article are closed. Why?

Site last updated: February 9, 2012