The following code shows how to create a new Business Contact and set the the value of the user-defined field named Favorite restaurant.
private static void CreateBusinessContactWithFavoriteRestaurant()
{
Outlook.ApplicationClass _app = new Outlook.ApplicationClass();
Outlook.Application olApp = (Outlook.Application)_app;
Outlook.NameSpace olNameSpace = _app.GetNamespace("MAPI");
Outlook.Folders folders = olNameSpace.Session.Folders;
Outlook.Folder bcmRootFolder = (Outlook.Folder)folders["Business Contact Manager"];
Outlook.Folder accounts = (Outlook.Folder)bcmRootFolder.Folders["Business Contacts"];
Outlook.UserProperty favoriterestaurant;
//Create a new instance of Outlook.ContactItem. Suffix the default Outlook
//Message Class IPM.Contact with BCM.Contact. This will be used by Outlook
//in publishing the BCM Contact form, if you open the newly created account.
Outlook.ContactItem newContact = (Outlook.ContactItem)accounts.Items.Add("IPM.Contact.BCM.Contact");
//Set Properties
newContact.FullName = "Jeff Smith";
newContact.FileAs = "Jeff Smith";
newContact.BusinessAddressStreet = "6789 S Lawrence Street";
newContact.BusinessAddressCity = "Roaring Fork Valley";
newContact.BusinessAddressState = "Colorado";
newContact.BusinessAddressPostalCode = "98052";
newContact.BusinessAddressCountry = "United States";
newContact.BusinessTelephoneNumber = "+1-800-123-4567";
newContact.Email1Address = "someone@example.com";
newContact.WebPage = "http://www.margiestravel.com";
favoriterestaurant = newContact.UserProperties.Add("Favorite restaurant", Microsoft.Office.Interop.Outlook.OlUserPropertyType.olText, false, System.Reflection.Missing.Value);
if (favoriterestaurant != null)
{
favoriterestaurant.Value = "Coho Vineyard";
}
//Calling the Save method on newContact object
//will create the new account
newContact.Save();
}
|
Sub CreateBusinessContactWithFavoriteRestaurant()
Dim olApp As Outlook.Application
Dim objNS As Outlook.NameSpace
Dim bcmRootFolder As Outlook.Folder
Dim olFolders As Outlook.Folders
Dim bcmContactsFldr As Outlook.Folder
Dim newContact As Outlook.ContactItem
Dim nationality As Outlook.UserProperty
Set olApp = CreateObject("Outlook.Application")
Set objNS = olApp.GetNamespace("MAPI")
Set olFolders = objNS.Session.Folders
Set bcmRootFolder = olFolders("Business Contact Manager")
Set bcmContactsFldr = bcmRootFolder.Folders("Business Contacts")
Set newContact = bcmContactsFldr.Items.Add("IPM.Contact.BCM.Contact")
newContact.FullName = "Jeff Smith"
newContact.FileAs = "Jeff Smith"
newContact.BusinessAddressStreet = "6789 S Lawrence Street"
newContact.BusinessAddressCity = "Roaring Fork Valley"
newContact.BusinessAddressState = "Colorado"
newContact.BusinessAddressPostalCode = "98052"
newContact.BusinessAddressCountry = "United States"
newContact.BusinessTelephoneNumber = "+1-800-123-4567"
newContact.Email1Address = "someone@example.com"
newContact.WebPage = "http://www.margiestravel.com"
Set nationality = newContact.UserProperties.Add("Favorite restaurant", olText)
favoriterestaurant.value = "Coho Vineyard"
newContact.Save
Set newContact = Nothing
Set bcmContactsFldr = Nothing
Set bcmRootFolder = Nothing
Set olFolders = Nothing
Set objNS = Nothing
Set olApp = Nothing
End Sub
|
See Also
Office Developer Center: Outlook 2007