Export (0) Print
Expand All

Changing Display Formats Programmatically for All Contacts in Outlook 2007

Office 2007

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

Summary:   Create an add-in to change the FileAs property programmatically for all contacts in Microsoft Office Outlook 2007.

Office Visual How To

Applies to:   2007 Microsoft Office System, Microsoft Office Outlook 2007, Microsoft Visual Studio 2005, Microsoft Visual Studio 2005 Tools for the 2007 Microsoft Office System Second Edition

Ken Getz, MCW Technologies, LLC

September 2007

Overview

Your Microsoft Office Outlook 2007 Contacts folder contains information about all your contacts (although it might contain other types of information, as well). Each contact item has information associated with it about how you want the item filed; that is, the FileAs property of the contact. You might want to programmatically enforce a specific setting for the FileAs property by using an add-in. This Office Visual How-to article explains how you can create an add-in by using Microsoft Visual Studio 2005 Tools for the 2007 Microsoft Office system Second Edition.

See It

Changing Display Formats Programmatically

Watch the Video

Length: 06:47 | Size: 4.9 MB | Type: WMV file

Code It | Read It | Explore It

You can standardize your contacts so that if a contact item contains a LastName value, you file it using LastName, FirstName format; and if the contact is a business without a LastName property, you file it using the CompanyName field.

Caution noteCaution

This example potentially modifies all the contacts in a specified Contacts folder. Make sure you either create a separate Contacts folder for testing or back up the Contacts folder before running the code in this article. Otherwise, you might modify a setting that you did not intend to change.

In this example, you create a Microsoft Office Fluent Ribbon customization that adds a button that is available when you view a contact. You click the button to modify the FileAs behavior of each contact.

To create a new FileAs button on the Ribbon

  1. In Visual Studio 2005 Tools for Office Second Edition, create an add-in for Outlook 2007. Name the new project ModifyFileAsAddin.

  2. In the Solution Explorer window, right-click the project, select Add from the context menu, and then click New Item.

  3. In the Add New Item dialog box, select Ribbon support. Click Add to insert the new Ribbon customization.

  4. Modify the new Ribbon1.xml file by replacing the existing content with the following code.

    <customUI xmlns="http://schemas.microsoft.com/office/2006/01/customui" 
        onLoad="OnLoad">
      <ribbon>
        <tabs>
          <tab idMso="TabAddIns">
            <group id="MyGroup"
                 label="File As">
              <button id="button1"
                      label="Modify File As"
                      screentip="Modify FileAs Behavior"
                      onAction="OnClick" />
            </group>
          </tab>
        </tabs>
      </ribbon>
    </customUI>
    
    
  5. In the Solution Explorer window, right-click the ModifyFileAsAddin project, and select Properties from the context menu.

  6. In the Properties pane, select the Resources tab.

  7. From the Solution Explorer window, drag the Ribbon1.xml file into the Properties pane.

    This action adds the Ribbon1.xml file as a project resource.

  8. Close the Properties pane, and click Yes when prompted to save.

  9. In the Ribbon1.vb or Ribbon1.cs file, uncomment the ThisAddIn partial class.

  10. In Microsoft Visual C# only, at the top of the code file, add the following using statement.

    using Outlook=Microsoft.Office.Interop.Outlook;
    
    
  11. In the Ribbon1 class, modify the existing GetCustomUI implementation so that it retrieves the Ribbon1 resource, and only returns the resource if the current ribbonID value is Microsoft.Outlook.Contact.

    In C#, you must expand the IRibbonExtensibility members code region to find the procedure.

    Public Function GetCustomUI(ByVal ribbonID As String) As String _
     Implements Office.IRibbonExtensibility.GetCustomUI
    
      If ribbonID = "Microsoft.Outlook.Contact" Then
        Return My.Resources.Ribbon1
      Else
        Return String.Empty
      End If
    End Function
    

    public string GetCustomUI(string ribbonID)
    {
      if (ribbonID == "Microsoft.Outlook.Contact")
      {
        return Properties.Resources.Ribbon1;
      }
      else
      {
        return String.Empty;
      }
    }
    
    
  12. In the Ribbon1 class, add the following declaration to make it simpler to refer to the Outlook Application object.

    Private Application As Outlook.Application = _
      Globals.ThisAddIn.Application
    

    private Outlook.Application Application = 
      Globals.ThisAddIn.Application;
    
    
  13. In the Ribbon1 class, add the following callback procedure. Click the new button on the Ribbon to call this procedure.

    Public Sub OnClick(ByVal control As Office.IRibbonControl)
    End Sub
    

    public void OnClick(Office.IRibbonControl control)
    {
    }
    
    
  14. Save the project, and run it.

  15. In Outlook 2007, create a new contact. On the Ribbon, click the Add-Ins tab, and verify that you see the new button on the Ribbon. Close Outlook when you finish, returning to Microsoft Visual Studio 2005.

  16. Start Outlook 2007, and select your Contacts folder.

  17. Select File, select Folder, and then select New Folder. In the Create New Folder dialog box, create a new folder for Contact Items named ContactsBackup.

  18. From your existing Contacts folder, copy some contacts into the ContactsBackup folder.

  19. Close Outlook 2007 when you finish.

  20. In the Ribbon1 class, add the following procedure.

    Private Sub RefileContacts(folder As Outlook.Folder)
      Dim items As Outlook.Items = folder.Items
      If items.Count > 0 Then
        Dim contactItems As Outlook.Items = _
         items.Restrict("[MessageClass]='IPM.Contact'")
    
        For Each item As Outlook.ContactItem In contactItems
          If item.LastName <> String.Empty Then
            item.FileAs = _
             String.Format("{0}, {1}", _
             item.LastName, item.FirstName)
          ElseIf Not String.IsNullOrEmpty(item.CompanyName) Then
            item.FileAs = item.CompanyName
          End If
    
          item.Save()
        Next
      End If
      MessageBox.Show("Your contacts have been re-filed.")
    End Sub
    

    private void RefileContacts(Outlook.Folder folder)
    {
      Outlook.Items items = folder.Items;
      if (items.Count > 0)
      {
        Outlook.Items contactItems = 
          items.Restrict("[MessageClass]='IPM.Contact'");
    
        foreach (Outlook.ContactItem itemContact in contactItems)
        {
          if (itemContact.LastName != String.Empty)
          {
            itemContact.FileAs = String.Format("{0}, {1}", 
              itemContact.LastName, itemContact.FirstName);
          }
          else if (itemContact.CompanyName != String.Empty) 
          {
            itemContact.FileAs = itemContact.CompanyName;
          }
          itemContact.Save();
        }
        MessageBox.Show("Your contacts have been re-filed.");
      }
    }
    
    
    

This procedure retrieves a reference to the items in the specified folder. If there are items in the folder (if the count is greater than 0), the code retrieves a restricted subset of items that includes only Contact items. (A Contacts folder can contain other items besides Contact items.) The code then loops through each item, and for each Contact, sets the FileAs property appropriately. Finally, the code calls the Save method of the Contact item, and stores it back in the Outlook data store.

To modify the existing OnClick procedure

  1. Modify the existing OnClick procedure, so that it retrieves a reference to a Contacts folder (in this case, the ContactsBackup folder you created earlier), calls the RefileContacts method, and passes the reference to the appropriate Contacts folder.

    Public Sub OnClick(ByVal control As Office.IRibbonControl)
      Dim folder As Outlook.Folder = _
      TryCast(Application.Session.GetDefaultFolder( _
       Outlook.OlDefaultFolders.olFolderContacts), Outlook.Folder)
    
      If folder IsNot Nothing Then
        folder = TryCast(folder.Folders("ContactsBackup"), _
         Outlook.Folder)
        If folder IsNot Nothing Then
          RefileContacts(folder)
        End If
      End If
    End Sub
    

    public void OnClick(Office.IRibbonControl control)
    {
      Outlook.Folder folder =
        Application.Session.
        GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts) 
        as Outlook.Folder;
    
      if (folder != null)
      {
        folder = folder.Folders["ContactsBackup"] as Outlook.Folder;
        if (folder != null)
        {
          RefileContacts(folder);
        }
      }
    }
    
  2. Save your project, and press F5 to run it.

  3. To edit an existing contact (or create a new contact), on the Ribbon, click Add-Ins, and click the new button.

    If you built the add-in correctly, you see the alert that indicates you refiled your contacts. Verify that the FileAs property is set appropriately for all contacts in the folder.

The Outlook 2007 object model provides several ways in which you can find a particular folder. To look for the default folder of any particular type, such as Inbox or Contacts, you can use the Application.Session.GetDefaultFolder method, passing in one of the OlDefaultFolders enumerated values. To look for a subfolder, you can retrieve a reference to the top-level folder, and use its Folders property to retrieve a specific subfolder. If you know the EntryID or the StoreID value for a specific folder, you can call the Application.Session.GetFolderFromID method.

Outlook provides several techniques for restricting the items within a folder. If your folder contains only a few items, the Items.Find method, passing in a filter string, provides the most efficient way to find a specific item. You can combine the Find method with the Items.FindNext method to search through all the matches for a particular filter. If, on the other hand, your folder contains a large number of items, the Restrict method provides a more efficient means of filtering a set of items. You can filter the items in a folder based on the message type, or you can filter based on specific value or values in one or more fields.

Show:
© 2014 Microsoft