How to: Modify the Layout of an Electronic Business Card

This example shows how to modify the layout of an Electronic Business Card by using the BusinessCardLayoutXml property of the ContactItem interface.

Programming Applications for Office Outlook 2007

The following code example is an excerpt from Programming Applications for Microsoft Office Outlook 2007, from Microsoft Press (ISBN 9780735622494, copyright Microsoft Press 2007, all rights reserved).

Buy this book

Sample chapters

An Electronic Business Card provides a contact view that captures specific information from that contact. The ContactItem interface provides specific members that pertain to Electronic Business Cards. These members are BusinessCardLayoutXml, BusinessCardType, AddBusinessCardLogoPicture(String), ForwardAsBusinessCard(), ResetBusinessCard(), SaveBusinessCardImage(String), and ShowBusinessCardEditor().

In the following code example, BusinessCardLayoutExample modifies the layout of an Electronic Business Card by first obtaining a specified ContactItem object. In this case, the ContactItem is a contact with the value of the Subject property equal to “Melissa MacBeth”. Next, BusinessCardLayoutExample creates an XML document class XmlDocument, and then gets the layout attribute of this class in a string by using the BusinessCardLayoutXML value for the ContactItem object. The card layout is then changed from left-aligned to right-aligned.

If you use Visual Studio to test this code example, you must first add a reference to the Microsoft Outlook 15.0 Object Library component and specify the Outlook variable when you import the Microsoft.Office.Interop.Outlook namespace. The using statement must not occur directly before the functions in the code example but must be added before the public Class declaration. The following line of code shows how to do the import and assignment in C#.

using Outlook = Microsoft.Office.Interop.Outlook;
private void BusinessCardLayoutExample()
{
    Outlook.ContactItem contact =
        Application.Session.GetDefaultFolder(
        Outlook.OlDefaultFolders.olFolderContacts).Items.Find(
        "[Subject] = Melissa MacBeth'")
        as Outlook.ContactItem;
    if (contact != null)
    {
        XmlDocument doc = new XmlDocument();
        doc.LoadXml(contact.BusinessCardLayoutXml);
        XmlElement root = doc.DocumentElement;
        string layoutValue = root.GetAttribute("layout");
        if (layoutValue == "left")
        {
            root.SetAttribute("layout", "right");
            contact.BusinessCardLayoutXml = doc.OuterXml;
            contact.Save();
        }
    }
}

Show:
© 2014 Microsoft