Microsoft KB Archive/310260

= How to use the Microsoft Outlook Object Library to retrieve a contact by using Visual C# =

Article ID: 310260

Article Last Modified on 6/29/2007

-

APPLIES TO


 * Microsoft Visual C# 2005
 * Microsoft Visual C# .NET 2003 Standard Edition
 * Microsoft Visual C# .NET 2002 Standard Edition
 * Microsoft Office Outlook 2003
 * Microsoft Outlook 2002 Standard Edition

-



This article was previously published under Q310260



INTRODUCTION
This article describes how to use the Microsoft Outlook 2002 Object Library or the Microsoft Office Outlook 2003 Object Library to retrieve a contact by using Microsoft Visual C#.



MORE INFORMATION
To use the Outlook 2002 Object Library or the Outlook 2003 Object Library to retrieve a contact by using Microsoft Visual C#, follow these steps:  In Microsoft Visual Studio .NET or in Microsoft Visual Studio 2005, create a new Console Application project:  On the File menu, point to New, and then click Project. Under Project Types, click Visual C# Projects.

Note In Visual Studio 2005, click Visual C# under Project Types. Under Templates, click Console Application. Click OK. By default, a file that is named Class1.cs is created.

Note In Visual Studio 2005, Program.cs is created by default.  Add a reference to either the Outlook 2002 Object Library or the Outlook 2003 Object Library. To do this, follow these steps:  On the Project menu, click Add Reference.</li> Click the COM tab.</li> On the COM tab, click Microsoft Outlook 11.0 Object Library if you are using Outlook 2003, or click Microsoft Outlook 10.0 Object Library if you are using Outlook 2002.</li> Click Select.

Note In Visual Studio 2005, you do not have to click Select.</li> In the Add References dialog box, click OK.</li> Note If you receive a message to generate wrappers for the libraries that you selected, click Yes.</li></ol> </li>  In the Class1.cs code window, replace all the existing code with the following code: using System; using System.Reflection; // to use Missing.Value //TO DO: If you use the Microsoft Outlook 11.0 Object Library, uncomment the following line. //using Outlook = Microsoft.Office.Interop.Outlook;

namespace ReadContact {  public class Class1 {     public static int Main(string[] args) {         try {           // Create the Outlook application. Outlook.Application oApp = new Outlook.Application;

// Get the NameSpace information. Outlook.NameSpace oNS = oApp.GetNamespace(&quot;mapi&quot;);

// Log on by using a dialog box to choose the profile. oNS.Logon(Missing.Value, Missing.Value, true, true); // Alternate logon method that uses a specific profile. // TODO: If you use this logon method, // change the profile name to an appropriate value. //oNS.Logon(&quot;YourValidProfile&quot;, Missing.Value, false, true); // Get the default Contacts folder. Outlook.MAPIFolder oContacts = oNS.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);

// Get the Items collection from the folder. Outlook.Items oItems = (Outlook.Items)oContacts.Items;

// Get the first contact item in the Items collection. Outlook.ContactItem oCt = (Outlook.ContactItem)oItems.GetFirst;

// Output 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);

// Display the contact. oCt.Display(true);

// Log off. oNS.Logoff;

// Explicitly release objects. oCt = null; oItems = null; oContacts = null; oNS = null; oApp = null; }

//Simple error handling. catch (Exception e)        { Console.WriteLine(&quot;{0} Exception caught.&quot;, e); }

//Default return value. return 0; }   } } </li> In this code, make any necessary changes where you see the &quot;TO DO&quot; comments.</li> Press F5 to build and then run the program.</li></ol>

<div class="references_section">