Share via


How to: Programmatically add an entry to Outlook contacts

Applies to: yesVisual Studio noVisual Studio for Mac

Note

This article applies to Visual Studio 2017. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

This example creates a new contact and enters data into the new contact.

Applies to: The information in this topic applies to VSTO Add-in projects for Outlook. For more information, see Features available by Office application and project type.

Example

Private Sub AddContact()
    Dim newContact As Outlook.ContactItem = Me.Application.CreateItem(Outlook. _
        OlItemType.olContactItem)
    Try
        With newContact
            .FirstName = "Jo"
            .LastName = "Berry"
            .Email1Address = "somebody@example.com"
            .CustomerID = "123456"
            .PrimaryTelephoneNumber = "(425)555-0111"
            .MailingAddressStreet = "123 Main St."
            .MailingAddressCity = "Redmond"
            .MailingAddressState = "WA"
            .Save()
            .Display(True)
        End With
    Catch
        MsgBox("The new contact was not saved.")
    End Try
End Sub
private void AddContact()
{
    Outlook.ContactItem newContact = (Outlook.ContactItem)
        this.Application.CreateItem(Outlook.OlItemType.olContactItem);
    try
    {
        newContact.FirstName = "Jo";
        newContact.LastName = "Berry";
        newContact.Email1Address = "somebody@example.com";
        newContact.CustomerID = "123456";
        newContact.PrimaryTelephoneNumber = "(425)555-0111";
        newContact.MailingAddressStreet = "123 Main St.";
        newContact.MailingAddressCity = "Redmond";
        newContact.MailingAddressState = "WA";
        newContact.Save();
        newContact.Display(true);
    }
    catch
    {
        MessageBox.Show("The new contact was not saved.");
    }
}

See also