How to retrieve contacts by using Outlook Object Model in Visual Basic .NET

Article translations Article translations
Article ID: 313802 - View products that this article applies to.
This article was previously published under Q313802
Expand all | Collapse all

On This Page

SUMMARY

The step-by-step article describes how to use the Outlook 10.0 Object Library to retrieve Outlook contacts in Visual Basic .NET.

Create Sample to Retrieve Outlook Contacts

  1. Start Microsoft Visual Studio .NET.
  2. On the File menu, point to New, and then click Project.
  3. Click Visual Basic Projects under Project Types, and then click Console Application under Templates. By default, Module1.vb is created.
  4. Add a reference to the Microsoft Outlook 10.0 Object Library. To do this, follow these steps:
    1. On the Project menu, click Add Reference.
    2. Click the COM tab.
    3. Click Microsoft Outlook 10.0 Object Library, and then click Select
    4. Click OK. If you are prompted to generate wrappers for the library that you selected, click Yes.
  5. In the Code window, replace the default code with the following code:
    Imports System.Reflection
    
    Module Module1
    
        Sub Main()
            ' 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", Missing.Value, False, True) ' TODO:
    
            ' 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.
                Console.WriteLine(oCt.FullName)
                Console.WriteLine(oCt.Title)
                Console.WriteLine(oCt.Birthday)
                Console.WriteLine(oCt.CompanyName)
                Console.WriteLine(oCt.Department)
                Console.WriteLine(oCt.Body)
                Console.WriteLine(oCt.FileAs)
                Console.WriteLine(oCt.Email1Address)
                Console.WriteLine(oCt.BusinessHomePage)
                Console.WriteLine(oCt.MailingAddress)
                Console.WriteLine(oCt.BusinessAddress)
                Console.WriteLine(oCt.OfficeLocation)
                Console.WriteLine(oCt.Subject)
                Console.WriteLine(oCt.JobTitle)
    
            Catch
    
                Console.WriteLine("an error occurred")
    
            Finally
    
                ' Display
                'oCt.Display(True)
    
                ' Log off.
                oNS.Logoff()
    
                ' Clean up.
                oApp = Nothing
                oNS = Nothing
                oItems = Nothing
                oCt = Nothing
    
            End Try
    
            
        End Sub
    
    End Module
    					
  6. Modify the code where you see the TODO comments.
  7. Press F5 to build and to run the application.
  8. Verify that the first contact is retrieved.

REFERENCES

For more information, visit the following Microsoft Developer Network (MSDN) Web site:
Microsoft Office Development with Visual Studio
http://msdn2.microsoft.com/en-us/library/aa188489(office.10).aspx

Properties

Article ID: 313802 - Last Review: January 17, 2007 - Revision: 4.3
APPLIES TO
  • Microsoft Office Outlook 2003
  • Microsoft Outlook 2002 Standard Edition
  • Microsoft Visual Basic .NET 2003 Standard Edition
  • Microsoft Visual Basic .NET 2002 Standard Edition
Keywords: 
kbhowtomaster KB313802

Give Feedback

 

Contact us for more help

Contact us for more help
Connect with Answer Desk for expert help.
Get more support from smallbusiness.support.microsoft.com