Microsoft KB Archive/313787

= How to use the Microsoft Outlook Object Library to create an Outlook contact in Visual Basic .NET =

Article ID: 313787

Article Last Modified on 3/15/2006

-

APPLIES TO


 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition
 * Microsoft Outlook 2002 Standard Edition
 * Microsoft Office Outlook 2003

-



This article was previously published under Q313787



SUMMARY
This article describes how to use the Microsoft Outlook Object Library to create a contact in Microsoft Visual Basic .NET.

This article assumes that you are familiar with the following topics:
 * Outlook Object Model (OOM)
 * Programming with Visual Basic .NET



MORE INFORMATION
To use the Microsoft Outlook Object Library to create a contact in Microsoft Visual Basic .NET, follow these steps:  Start Microsoft Visual Studio .NET. On the File menu, point to New, and then click Project. Under Project Types, click Visual Basic Projects. Under Templates, click Console Application, and then click OK.

By default, Module1.vb is created. Add a reference to the Microsoft Outlook 10.0 Object Library. To do this, follow these steps:  On the Project menu, click Add Reference. On the COM tab, click Microsoft Outlook 10.0 Object Library, and then click Select.</li> In the Add References dialog box, click OK to accept your selections. If you receive a prompt to generate wrappers for the libraries that you selected, click Yes.</li></ol> </li>  In the code window for Module1, replace all the code with: 'TO DO: If you use the Microsoft Outlook 11.0 Object Library, uncomment the following line. 'Imports Outlook = Microsoft.Office.Interop.Outlook Imports System.Reflection

Module Module1

Sub Main ' Create an Outlook application. Dim oApp As Outlook.Application = New Outlook.Application

' Get the namespace and the logon. Dim oNS As Outlook.NameSpace = oApp.GetNamespace(&quot;MAPI&quot;) ' TODO: Replace the &quot;YourValidProfile&quot; and &quot;myPassword&quot; with 'Missing.Value if you want to log on with the default profile. oNS.Logon(&quot;YourValidProfile&quot;, &quot;myPassword&quot;, True, True)

' Create a new contact item. Dim oCt As Outlook.ContactItem = oApp.CreateItem(Outlook.OlItemType.olContactItem) oCt.Display(True) 'Modal

' Set some common properties. oCt.FullName = &quot;David Pelton&quot; oCt.Title = &quot;Student&quot; oCt.Birthday = Convert.ToDateTime(&quot;10/1/1982&quot;) oCt.CompanyName = &quot;Fourth Coffee &quot; oCt.Department = &quot;PSS&quot; oCt.Body = &quot;Test Body&quot; oCt.FileAs = &quot;David&quot; oCt.Email1Address = &quot;abc@hotmail.com&quot; oCt.BusinessHomePage = &quot;http://www.fourthcoffee.com/&quot; oCt.MailingAddress = &quot;12345 Bellevue&quot; oCt.BusinessAddress = &quot;56789 1st. Redmond, WA 98001&quot; oCt.OfficeLocation = &quot;666 Office&quot; oCt.Subject = &quot;Hello World&quot; oCt.JobTitle = &quot;Engineer&quot;

' Save it to the Contacts folder. oCt.Save

' Display the created contact item. 'oCt.Display(True)

' Log off. oNS.Logoff

' Clean up. oApp = Nothing oNS = Nothing oCt = Nothing End Sub

End Module </li> Search for &quot;TO DO&quot; in the code, and then modify the code for your environment.</li> Press F5 to build and to run the program.</li> Verify that the contact has been created.</li></ol>

<div class="references_section">