Class: OutlookAddressBook in Category Microsoft Outlook : Automation from Total Visual SourceBook

Working with the Microsoft Outlook Address Book and the Exchange Server Global Address List through Automation using VBA and VB6.

Procedure Name Type Description
(Declarations) Declarations Declarations and private variables for the COutlookAddressBook class.
AddressListName Property Get the value of the Global Address List Property.
AppNameSpace Property Get a handle to the current instance of the Outlook NameSpace.
AppOutlook Property Get a handle to the current instance of Outlook.
Class_Initialize Initialize Set initial values to defaults which may be overridden with property settings.
Class_Terminate Terminate Clean up class variables opened for Outlook.
CloseOutlook Method Close an instance of Outlook.
GetAddressLists Method Get all the address lists in the Outlook account.
GetAddressListItems Method Get all items in the Address List. Uses the AddressList property (defaults to the Global Address List).
GetDistributionListMembers Method Create a collection object to hold the Address Entries in the Distribution List. Uses the AddressList property (defaults to the Global Address List).
GetDistributionLists Method Get All Entries in the Address List which are Distribution List Entries. Uses the AddressList property (defaults to the Global Address List).
IsNameInAddressList Method Search for a specific name in the Address List.
StartOutlook Method Starts an instance of Outlook.
' Example of the COutlookAddressBook class
'
' To use this example:
' 1. Create a new form
' 2. Create these text boxes:
'       txtDistListName
' 3. Create these command buttons
'       cmdGetAddressLists
'       cmdGetAddressListMembers
'       cmdGetDistLists
'       cmdGetListMembers
'       cmdIsAddressInList
' 4. Paste all the code from this example to the new form's module.

Private Sub cmdGetAddressLists_Click()
  ' Comments: Get the address list names from your address book

  Dim clsOutlookAddressBook As COutlookAddressBook
  Dim colAddressLists As New Collection
  Dim intCounter As Integer
  Dim strMsg As String

  ' Initialize Address Book class and connect to Outlook
  Set clsOutlookAddressBook = New COutlookAddressBook
  clsOutlookAddressBook.StartOutlook

  ' Get the Address Lists
  Set colAddressLists = clsOutlookAddressBook.GetAddressLists

  strMsg = "Address Lists:" & vbCrLf
  For intCounter = 1 To colAddressLists.Count
    strMsg = strMsg & colAddressLists.Item(intCounter) & vbCrLf
  Next intCounter

  Set colAddressLists = Nothing

  MsgBox strMsg

End Sub

Private Sub cmdGetAddressListMembers_Click()
  ' Comments: Get the items from an address list in your address book

  Const cintMax As Integer = 20
  Dim intResponse As Integer
  Dim fBasic As Boolean
  Dim clsOutlookAddressBook As COutlookAddressBook
  Dim colAddresses As New Collection
  Dim intCounter As Integer
  Dim strMsg As String

  intResponse = MsgBox("Do you want to retrieve the Basic or Detailed address information? Select Yes for Basic, No for Detailed.", vbQuestion + vbYesNoCancel)
  If intResponse <> vbCancel Then
    fBasic = (intResponse = vbYes)

    ' Initialize Address Book class and connect to Outlook
    Set clsOutlookAddressBook = New COutlookAddressBook
    clsOutlookAddressBook.StartOutlook

    ' By default, it uses the "Global Address List" which can be changed with the AddressListName property:
    clsOutlookAddressBook.AddressListName = "Contacts"

    ' Get the list of items for the address list.
    ' For this example, a limit is set to the maximum number of items to retrieve.
    Set colAddresses = clsOutlookAddressBook.GetAddressListItems(fBasic, cintMax)

    ' Clean up
    Set clsOutlookAddressBook = Nothing

    ' Get the information from the collection that was returned
    For intCounter = 1 To colAddresses.Count
      If fBasic Then
        strMsg = strMsg & colAddresses.Item(intCounter).name & vbCrLf
      Else
        With colAddresses.Item(intCounter)
          ' Details for an Exchange User
          If .Class = olContact Then
            Debug.Print intCounter, .Subject, .Email1Address, .BusinessAddressStreet, .BusinessAddressCity & " " & .BusinessAddressState & " " & .BusinessAddressPostalCode, .CompanyName, .Department, .JobTitle
          ElseIf .Class = olExchangeUser Then
            Debug.Print intCounter, Trim$(.FirstName & " " & .LastName), .PrimarySmtpAddress, .StreetAddress, .City & " " & .StateOrProvince & " " & .PostalCode, .CompanyName, .Department, .JobTitle
          End If
        End With
      End If
    Next intCounter

    Set colAddresses = Nothing

    If fBasic Then
      MsgBox strMsg, vbInformation, "Basic Address List Items"
    Else
      Debug.Print
      MsgBox "See the Detailed Address List Items in the Immediate Window [Ctrl G]", vbInformation
    End If
  End If
End Sub

Private Sub cmdGetDistLists_Click()
  ' Comments: Get the distribution lists from your address book

  Dim clsOutlookAddressBook As COutlookAddressBook
  Dim colAddresses As New Collection
  Dim intCounter As Integer
  Dim strMsg As String

  ' Initialize Address Book class and connect to Outlook
  Set clsOutlookAddressBook = New COutlookAddressBook
  clsOutlookAddressBook.StartOutlook

  ' By default, it uses the "Global Address List" which can be changed with the AddressListName property:
  'clsOutlookAddressBook.AddressListName = "Contacts"

  Set colAddresses = clsOutlookAddressBook.GetDistributionLists

  ' Clean up
  Set clsOutlookAddressBook = Nothing

  strMsg = "Distribution Lists:" & vbCrLf
  For intCounter = 1 To colAddresses.Count
    strMsg = strMsg & colAddresses.Item(intCounter).name & "; "
  Next intCounter

  ' Set default distribution list to use when getting its members
  If colAddresses.Count > 0 Then
    If Nz(Me.txtDistListName) = "" Then
      Me.txtDistListName = colAddresses.Item(1).name
    End If
  End If

  Set colAddresses = Nothing

  MsgBox strMsg

End Sub

Private Sub cmdGetListMembers_Click()
  ' Comments: Get the members in each distribution list from your address book

  Dim clsOutlookAddressBook As COutlookAddressBook
  Dim colAddresses As New Collection
  Dim intCounter As Integer
  Dim strList As String
  Dim strMsg As String

  strList = Nz(Me.txtDistListName)
  strList = InputBox("Enter the distribution list whose members to retrieve", , strList)
  If strList <> "" Then

    ' Initialize Address Book class and connect to Outlook
    Set clsOutlookAddressBook = New COutlookAddressBook
    clsOutlookAddressBook.StartOutlook

    ' By default, it uses the "Global Address List" which can be changed with the AddressListName property:
    'clsOutlookAddressBook.AddressListName = "Contacts"

    Set colAddresses = clsOutlookAddressBook.GetDistributionListMembers(strList)

    ' Clean up
    Set clsOutlookAddressBook = Nothing

    strMsg = "Members of Distribution List [" & strList & "]:" & vbCrLf
    For intCounter = 1 To colAddresses.Count
      strMsg = strMsg & colAddresses.Item(intCounter).name & "; "
    Next intCounter

    Set colAddresses = Nothing

    MsgBox strMsg

  End If

End Sub

Private Sub cmdIsAddressInList_Click()
  ' Comments: See if a name is in the address book

  Dim clsOutlookAddressBook As COutlookAddressBook
  Dim strName As String
  Dim fOK As Boolean

  strName = InputBox("Enter the name to check in your Address list:")

  If strName <> "" Then

    ' Initialize Address Book class and connect to Outlook
    Set clsOutlookAddressBook = New COutlookAddressBook
    clsOutlookAddressBook.StartOutlook

    ' By default, it uses the "Global Address List" which can be changed with the AddressListName property:
    clsOutlookAddressBook.AddressListName = "Contacts"

    fOK = clsOutlookAddressBook.IsNameInAddressList(strName)

    ' Clean up
    Set clsOutlookAddressBook = Nothing

    If fOK Then
      MsgBox strName & " was found in the address list"
    Else
      MsgBox strName & " was not found in the address list"
    End If
  End If

End Sub

Private Sub Form_Load()
  Const cintLeft = 4500
  Const cintWidth = 3000

  ' Setup controls
  With Me.txtDistListName
    .Value = ""
    .Top = 250
    .Left = 250
    .Width = 4000
  End With

  ' Command buttons
  With Me.cmdGetAddressLists
    .Caption = "Address Lists"
    .Top = 100
    .Left = cintLeft
    .Width = cintWidth
  End With

  With Me.cmdGetAddressListMembers
    .Caption = "Address List Members"
    .Top = 600
    .Left = cintLeft
    .Width = cintWidth
  End With

  With Me.cmdGetDistLists
    .Caption = "Distribution Lists"
    .Top = 1100
    .Left = cintLeft
    .Width = cintWidth
  End With

  With Me.cmdGetListMembers
    .Caption = "Distribution Members"
    .Top = 1600
    .Left = cintLeft
    .Width = cintWidth
  End With

  With Me.cmdIsAddressInList
    .Caption = "Is Address In List"
    .Top = 2100
    .Left = cintLeft
    .Width = cintWidth
  End With
End Sub

Total Visual SourceBook The source code in Total Visual Sourcebook includes modules and classes for Microsoft Access, Visual Basic 6 (VB6), and Visual Basic for Applications (VBA) developers. Easily add this professionally written, tested, and documented royalty-free code into your applications to simplify your application development efforts.

Total Visual SourceBook is written for the needs of a developer using a source code library covering the many challenges you face. Countless developers over the years have told us they learned some or much of their development skills and tricks from our code. You can too!

Additional Resources

Total Visual SourceBook CD and Printed Manual

Microsoft Access/ Office 2016, 2013, 2010, and 2007 Version
is Shipping!

New features in Total Visual SourceBook for Access, Office and VB6

Supports Access/Office 2016, 2013, 2010 and 2007, and Visual Basic 6.0!


View all FMS products for Microsoft Access All Our Microsoft Access Products

Reviews

Reader Choice Award for MS Access Source Code Library
Reader Choice

"The code is exactly how I would like to write code and the algorithms used are very efficient and well-documented."

Van T. Dinh, Microsoft MVP

SourceBook Info

Additional Info

Question

 

 

Free Product Catalog from FMS