vb.netemailoutlookvb.net-2010import-contacts

Return list of names and email address from outlook to vb.net listbox


I want to return from outlook, a list of names and email address and populate them in a listbox so that I can select the ones I want.

I'm looking to do this from the users local contact list and also the global address list on an exchange server.

I've seen many examples (Below) and nothing works, so any help would be most welcomed.

Thanks

Graham

I am using

Imports Microsoft.Office.Interop.Outlook
Imports Microsoft.Office.Interop

for both examples:

 Dim itemx As ListViewItem

        'Create an Outlook application.
        Dim oApp As Outlook._Application = New Outlook.Application()

        ' Get the MAPI namespace.
        Dim oNS As Outlook.NameSpace = oApp.Session
        ' Get the Global Address List.
        Dim oALs As Outlook.AddressLists = oNS.AddressLists
        Dim oGal As Outlook.AddressList = oALs.Item("Global Address List")

        ' Get all the entries.
        Dim oEntries As Outlook.AddressEntries = oGal.AddressEntries
        ' Get the first user.
        Dim oEntry As Outlook.AddressEntry = oEntries.GetFirst

        For i As Long = 1 To 10 ' Cut down to 100 as I dont want to load the full AB ** Need to Search rather than Loop **
            If oEntries(i).DisplayType = Outlook.OlDisplayType.olUser Then
                itemx = ListView1.Items.Add(oEntries(i).Name)
                itemx.SubItems.Add(oEntries(i).GetExchangeUser.JobTitle)
                itemx.SubItems.Add(oEntries(i).GetExchangeUser.BusinessTelephoneNumber)
                itemx.SubItems.Add(oEntries(i).GetExchangeUser.OfficeLocation)
                itemx.SubItems.Add(oEntries(i).GetExchangeUser.PrimarySmtpAddress)
                itemx.SubItems.Add(oEntries(i).GetExchangeUser.CompanyName)
                itemx.SubItems.Add(oEntries(i).GetExchangeUser.Alias)
            End If
        Next

        ' Clean up.
        oApp = Nothing
        oNS = Nothing
        oALs = Nothing
        oGal = Nothing
        oEntries = Nothing
        oEntry = Nothing

Also tried:

' Create Outlook application.
Dim oApp As Outlook.Application = New Outlook.Application()
'Get NameSpace and Logon.
Dim oNS As Outlook.NameSpace = oApp.GetNamespace("mapi")
'oNS.Logon("Outlook", , False, True) ' TODO:
oNS.Logon("Outlook", Nothing, False, True)
' Get the first contact from the Contacts folder.
Dim cContacts As Outlook.MAPIFolder = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
Dim oItems As Outlook.Items = cContacts.Items
Dim oCt As Outlook.ContactItem

Try
  oCt = oItems.GetFirst()
  ' Display some common properties.
  ListBox1.Items.Add(oCt.FullName)
  ListBox1.Items.Add(oCt.Title)
  ListBox1.Items.Add(oCt.Birthday)
  ListBox1.Items.Add(oCt.CompanyName)
  ListBox1.Items.Add(oCt.Department)
  ListBox1.Items.Add(oCt.Body)
  ListBox1.Items.Add(oCt.FileAs)
  ListBox1.Items.Add(oCt.Email1Address)
  ListBox1.Items.Add(oCt.BusinessHomePage)
  ListBox1.Items.Add(oCt.MailingAddress)
  ListBox1.Items.Add(oCt.BusinessAddress)
  ListBox1.Items.Add(oCt.OfficeLocation)
  ListBox1.Items.Add(oCt.Subject)
  ListBox1.Items.Add(oCt.JobTitle)
  Catch
      ListBox1.Items.Add("an error occurred")
     'Finally

     ' Display
     'oCt.Display(True)
     ' Log off.
     oNS.Logoff()
    ' Clean up.
     oApp = Nothing
     oNS = Nothing
     oItems = Nothing
     oCt = Nothing
  End Try

Solution

  • It appears that the sample code in your first example is correct, except for the "Global Address List" access string. Try using 1 as the array index:

    Dim oGal As AddressList = oALs.Item(1)
    

    I was able to access my contacts by doing this.