Hi there,

I have managed to access my Outlook contacts that are stored in the "Contacts" folder, but what I really want to access is the contacts that appear when you start to type in the "To" field in a new email.

Is this even possible..? If anyone has any suggestions it would be much appreciated.

Thank you.

vb Code:
  1. Imports Microsoft.Office.Interop
  2.  
  3. Public Class Form1
  4.     'Declare Objects of Outlook using Outlook._Application interface
  5.     Dim objOutlook As Outlook._Application
  6.  
  7.     'Outlook Namespace will be current session
  8.     Dim objNS As Outlook._NameSpace
  9.  
  10.     Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
  11.         Try
  12.             'Initialise objects created in Form Load
  13.             objOutlook = New Outlook.Application()
  14.             objNS = objOutlook.Session
  15.  
  16.             'Get the Contact folder
  17.             Dim objAddressList As Outlook.Folder
  18.             objAddressList = objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
  19.  
  20.             'Get all the contacts
  21.             Dim objItems As Outlook.Items = objAddressList.Items
  22.             Dim objContact As Outlook.ContactItem
  23.  
  24.             'Loop through all contacts and add to combo box
  25.             Dim j As Integer
  26.             For j = 1 To objItems.Count
  27.                 objContact = CType(objItems(j), Outlook.ContactItem)
  28.                 ComboBox1.Items.Add(objContact.FullName)
  29.             Next
  30.  
  31.             'clear memory
  32.             objContact = Nothing
  33.             objItems = Nothing
  34.             objAddressList = Nothing
  35.         Catch ex As Exception
  36.             MessageBox.Show(ex.Message)
  37.         End Try
  38.     End Sub
  39.  
  40.     Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
  41.         Try
  42.             'get the contacts folder
  43.             Dim objAddressList As Outlook.Folder
  44.             objAddressList = objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)
  45.  
  46.             'Find the contact by his full name
  47.             Dim objItems As Outlook.Items = objAddressList.Items
  48.             Dim objContact As Outlook.ContactItem = CType(objItems.Find("[FullName] = '" & ComboBox1.SelectedItem.ToString() & "'"), Outlook.ContactItem)
  49.  
  50.             'Display details in labels
  51.             Label1.Text = objContact.FirstName
  52.             Label2.Text = objContact.LastName
  53.             Label3.Text = objContact.Email2Address
  54.  
  55.             ''free the memory
  56.             objContact = Nothing
  57.             objItems = Nothing
  58.             objAddressList = Nothing
  59.         Catch ex As Exception
  60.             MessageBox.Show(ex.Message)
  61.         End Try
  62.     End Sub
  63.  
  64.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
  65.         Dim tempApp As New Outlook.Application()
  66.         Dim tempNS As Outlook.NameSpace
  67.         Dim MailFolder As Outlook.Folder
  68.  
  69.         tempNS = tempApp.GetNamespace("MAPI")
  70.         tempNS.Logon(, , True, True)
  71.  
  72.         Dim newMail As Outlook.MailItem
  73.         MailFolder = tempNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderOutbox)
  74.         newMail = CType(MailFolder.Items.Add(Outlook.OlItemType.olMailItem), Outlook.MailItem)
  75.  
  76.         ' sent email
  77.         newMail.Subject = TextBox1.Text
  78.         newMail.Body = TextBox2.Text
  79.         newMail.To = Label3.Text
  80.         newMail.SaveSentMessageFolder = MailFolder
  81.         newMail.Send()
  82.         MessageBox.Show("send successfully")
  83.     End Sub
  84.  
  85.     'Dispose objects create objects in form dispose
  86.  
  87.     Private Sub Form1_Disposed(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Disposed
  88.         objOutlook = Nothing
  89.         objNS = Nothing
  90.     End Sub
  91.  
  92. End Class