Resources » .NET programming » .NET Framework

Reading Outlook Contacts in VB.NET


Posted Date: 06-May-2009  Last Updated:   Category: .NET Framework    
Author: Member Level: Gold    Points: 12



In this article I am trying to read the Outlook contact into a Combo Box and selecting a contact I am displaying his/ her details in below Label controls, so lets start building example.

Create a Windows Forms Application and Add Label and Combo Box and change the lable text to Select Contact.

To use the Outlook object in our project we need include a dll from the add references, now add a reference and select "Microsoft.Office.Interop.Outlook" dll from the list.

After adding the dll, now we will import the name space for that use following import

Imports Microsoft.Office.Interop


To access Outlook we need to create a object for Outlook via its interface and to get current session we need a NameSpace object to create these object use following code

'Declare Objects of Outlook using Outlook._Application interface
Dim objOutlook As Outlook._Application
'Outlook Namespace will be current session
Dim objNS As Outlook._NameSpace


Now in the Form Load we will initialise the objects and fill the combo box with contact names, for that in the Form_Load use following code

Private Sub Form6_Load(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles MyBase.Load
Try
'Initialise objects created in Form Load
objOutlook = New Outlook.Application()
objNS = objOutlook.Session

'Get the Contact folder
Dim objAddressList As Outlook.MAPIFolder
objAddressList = _
objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)

'Get all the contacts
Dim objItems As Outlook.Items = objAddressList.Items
Dim objContact As Outlook.ContactItem

'Loop through all contacts and add to combo box
For j = 1 To objItems.Count
objContact = objItems(j)
ComboBox1.Items.Add(objContact.FullName)
Next

'clear memory
objContact = Nothing
objItems = Nothing
objAddressList = Nothing
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub


Once we get the list of contacts in the Combo box use will select a Contact on selecting a contact we will display his or her details in the labels we put on the form for that we need to add following coding in the ComboBox_SelectedIndexChanged event, basically we are searching through all contacts and get the selected contact and display his or her details

'Selecting a contact should display his name email
Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, _
ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
Try
'get the contacts folder
Dim objAddressList As Outlook.MAPIFolder
objAddressList = objNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts)

'Find the contact by his full name
Dim objItems As Outlook.Items = objAddressList.Items
Dim objContact As Outlook.ContactItem = objItems.Find("[FullName] = '" & _
ComboBox1.SelectedItem.ToString() & "'")
'Display details in labels
Label7.Text = objContact.FirstName
Label6.Text = objContact.LastName
Label5.Text = objContact.Email1Address

'free the memory
objContact = Nothing
objItems = Nothing
objAddressList = Nothing
Catch ex As Exception
MessageBox.Show(ex.Message)
End Try
End Sub


Now we need to clear the object memory we used in the project in Form_Disposed event

'Dispose objects create objects in form dispose
Private Sub Form6_Disposed(ByVal sender As Object, _
ByVal e As System.EventArgs) Handles Me.Disposed
objOutlook = Nothing
objNS = Nothing
End Sub


Now we are ready with our code just build, run and test

I have attached code screen shot with this article for better understing

Hope this helps


Cheers
SatishKumar J
Microsoft MVP(ASP.NET)


Did you like this resource? Share it with your friends and show your love!

Responses to "Reading Outlook Contacts in VB.NET"

No responses found. Be the first to respond...

Feedbacks      

Post Comment:




  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:   Sign In to fill automatically.
    Email: (Will not be published, but required to validate comment)



    Type the numbers and letters shown on the left.


    Submit Article     Return to Article Index

    Subscribe to Subscribers
    Active Members
    TodayLast 7 Daysmore...

    Awards & Gifts
    Talk to Webmaster Tony John

    Online Members

    Priya jain
    More...
    Copyright © SpiderWorks Technologies Pvt Ltd., Kochi, India