How to: Read Data from a Custom Field of an Item

Applies to

The information in this topic applies only to the specified Visual Studio Tools for Office projects and versions of Microsoft Office.

Project type

  • Application-level projects

Microsoft Office version

  • Outlook 2003

  • Outlook 2007

For more information, see Features Available by Application and Project Type.

This example reads data from a custom field of a contact item. This example checks contacts in a folder named Picnic for a value in a custom text field named Picnic. The code displays the name of all contacts that have the value Potato Salad.

Example

Private Sub ThisAddIn_Startup(ByVal sender As Object, _
 ByVal e As System.EventArgs) Handles Me.Startup
    Dim result As New System.Text.StringBuilder()
    Dim contactFolder As Outlook.MAPIFolder = Me.Application.GetNamespace _
        ("MAPI").GetDefaultFolder _
        (Outlook.OlDefaultFolders.olFolderContacts). _
        Folders("Picnic")

    Dim picnicMembers As Outlook.Items = _
        contactFolder.Items.Restrict("[Picnic]='Potato Salad'")

    For Each contact As Outlook.ContactItem In picnicMembers
        result.AppendLine(contact.FullName)
    Next 

    If result.Length > 0 Then
        MessageBox.Show(result.ToString(), "Potato Salad")
    Else
        MessageBox.Show( _
            "No contacts are assigned potato salad for the picnic.", _
            "Potato Salad")
    End If 
End Sub
private void ThisAddIn_Startup(object sender, 
    System.EventArgs e)
{
    System.Text.StringBuilder result =
        new System.Text.StringBuilder();

    Outlook.MAPIFolder contactFolder = this.
        Application.GetNamespace("MAPI").
        GetDefaultFolder(Outlook.OlDefaultFolders.
        olFolderContacts).Folders["Picnic"];
    Outlook.Items picnicMembers = contactFolder.Items.
        Restrict("[Picnic]='Potato Salad'");
    foreach (Outlook.ContactItem contact in picnicMembers)
    {
        result.AppendLine(contact.FullName);
    }

    if (result.Length > 0)
    {
        MessageBox.Show(result.ToString(), "Potato Salad");
    }
    else
    {
        MessageBox.Show("No contacts are assigned potato salad"
            + " for the picnic.", "Potato Salad");
    }
}

Robust Programming

A COMException exception is thrown if the custom contact folder Picnic is not found in the folders collection. You can catch this exception and create the folder if it is missing.

See Also

Tasks

How to: Move Items in Outlook

How to: Create Custom Folder Items

How to: Retrieve a Folder by Name

Concepts

Outlook Object Model Overview