Is there a way to get all contacts id's from outlook 2013 or 2007 to datagridview or combobox.collection list.

Thanks

Dani AI

Generated

If you need IDs from the Global Address List (GAL) rather than your personal Contacts, iterate Outlook’s GAL and read each AddressEntry. The unique key you want is AddressEntry.ID. The quick sample below loads Id, Name, and primary SMTP into a DataTable you can bind to a DataGridView or ComboBox. This must run on a machine with Outlook configured for an Exchange profile (as noted, the earlier snippet targets the Contacts folder, not the GAL).

Sub LoadGal()
    Dim app = New Microsoft.Office.Interop.Outlook.Application()
    Dim sess = app.Session
    Dim gal = sess.GetGlobalAddressList()
    If gal Is Nothing Then Throw New InvalidOperationException("No GAL available. Are you connected to Exchange?")

    Dim dt As New DataTable()
    dt.Columns.Add("Id") : dt.Columns.Add("Name") : dt.Columns.Add("Email")

    For Each ae As Microsoft.Office.Interop.Outlook.AddressEntry In gal.AddressEntries
        Dim id = ae.ID
        Dim name = ae.Name
        Dim email As String = Nothing

        Dim exu = ae.GetExchangeUser()
        If exu IsNot Nothing Then email = exu.PrimarySmtpAddress
        If String.IsNullOrEmpty(email) Then
            Const PR_SMTP As String = "http://schemas.microsoft.com/mapi/proptag/0x39FE001E"
            Try : email = TryCast(ae.PropertyAccessor.GetProperty(PR_SMTP), String) : Catch : End Try
        End If

        dt.Rows.Add(id, name, email)
    Next

    DataGridView1.DataSource = dt
    ComboBox1.DisplayMember = "Name" : ComboBox1.ValueMember = "Id" : ComboBox1.DataSource = dt
End Sub

Notes and gotchas:

Recommended Answers

All 6 Replies

Yes. There are a number of APIs out there, including one that comes with Office, that will support this functionality.

Add reference to "Microsoft Outlook xx.x Object Library" (where xx.x = 14.0, 12.0, etc):
* Click "Project"
* Select "Add Reference"
* Click "COM"
* Select "Microsoft Outlook 14.0 Object Library"

Add Imports statement:

  • Imports Outlook = Microsoft.Office.Interop.Outlook;

getContacts:

Private Sub getContacts()

    'create new instance of Outlook.Application
    Dim outlookApp As Outlook.Application = New Outlook.Application()

    'create new instance of Outlook.MAPIFolder
    Dim outlookMapiFolder As Outlook.MAPIFolder = Nothing

    'create new instance of Outlook.Items
    Dim outlookItems As Outlook.Items = Nothing

    'set outlookMapiFolder to Contacts folder
    outlookMapiFolder = outlookApp.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)

    'get items from contacts
    outlookItems = outlookMapiFolder.Items


    For i As Integer = 0 To outlookItems.Count - 1

        Dim contact As Outlook.ContactItem = outlookItems(i + 1)


        Dim entryId As String = contact.EntryID
        Dim fullName As String = contact.FullName
        Dim companyName As String = contact.CompanyName
        Dim email1Address As String = contact.Email1Address
        Dim email1AddressType As String = contact.Email1AddressType
        Dim businessTelephoneNumber As String = contact.BusinessTelephoneNumber
        Dim homeTelephoneNumber As String = contact.HomeTelephoneNumber
        Dim mobileTelephoneNumber As String = contact.MobileTelephoneNumber

        'add any additional entries that you want to retrieve



        'add entryId and fullName to dataGridView1
        'where DataGridView1 is a DataGridView with
        'columns "entryId" and "fullName"
        DataGridView1.Rows.Add(entryId, fullName)

    Next
End Sub

Resources:
Adapted from: Get Outlook Contacts in to C# form based Application

Outlook Object Model Overview

thanks

i want to get contacts from Global Address List

i just want to retrive contact list no modification to Global Address List.

how to specify Global Address List folder name.
thanks

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.