Pieter van der Westhuizen

Advanced Outlook form regions: customize an e-mail form

Today, I would like to show you how to add extra functionality to the Outlook Inspector objects using advanced Outlook form regions and Add-in Express 2010.

Start by creating a new ADX COM Add-in project in Visual Studio 2010.

Creating a new ADX COM Add-in Project in Visual Studio 2010

Select Visual C# for the programming language and Microsoft Office 2010 as the Minimum supported Office version and then select Microsoft Office in the Supported Applications list.

Customizing an Outlook e-mail form

We’ll add a region to the bottom of the Outlook e-mail form which will display a list of meetings organized by the person who sent us the e-mail. To achieve this we first need to add a new ADX Outlook form to our project, by selecting Add New Item… from Visual Studio’s Project menu.

Adding a new Add-in Express Outlook Form

Add a Listview component to the form, and change the design to represent something similar to the screenshot below.

Adding a Listview component to the Outlook Form

Add the following code to the form’s Load event:

private void EmailInfoForm_Load(object sender, EventArgs e)
{
Outlook._Application OutlookApp = this.OutlookAppObj as Outlook._Application;
Outlook._Inspector currInspector = this.InspectorObj as Outlook._Inspector;
Outlook._NameSpace nameSpace = OutlookApp.GetNamespace("MAPI");
Outlook.MAPIFolder calendarFolder = nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderCalendar);
Outlook.Items calendarItems = calendarFolder.Items;
 
string senderName = string.Empty;
 
if (currInspector.CurrentItem != null && currInspector.CurrentItem is Outlook.MailItem)
{
    Outlook.MailItem currMail = currInspector.CurrentItem as Outlook.MailItem;
    senderName = currMail.SenderName;
    string propertyTag = "https://schemas.microsoft.com/mapi/proptag/0x0c1a001f";
    string restriction = String.Format("@SQL=({0}{1}{0} CI_PHRASEMATCH '{2}')", (char)34, propertyTag, senderName);
    Outlook.Items restrictedItems = calendarItems.Restrict(restriction);
 
    for (int i = 1; i <= restrictedItems.Count; i++)
    {
        Outlook._AppointmentItem meeting = restrictedItems[i] as Outlook._AppointmentItem;
        ListViewItem lviItem = new ListViewItem(meeting.Subject);
        ListViewItem.ListViewSubItem lvsiDate = new ListViewItem.ListViewSubItem(lviItem, meeting.Start.ToShortDateString());
        ListViewItem.ListViewSubItem lvsiStartTime = new ListViewItem.ListViewSubItem(lviItem, meeting.Start.ToShortTimeString());
        ListViewItem.ListViewSubItem lvsiEndTime = new ListViewItem.ListViewSubItem(lviItem, meeting.End.ToShortTimeString());
        ListViewItem.ListViewSubItem lvsiDuration = new ListViewItem.ListViewSubItem(lviItem, (Convert.ToDecimal(meeting.Duration) / 60).ToString() + " hour(s)");
        lviItem.SubItems.Add(lvsiDate);
        lviItem.SubItems.Add(lvsiStartTime);
        lviItem.SubItems.Add(lvsiEndTime);
        lviItem.SubItems.Add(lvsiDuration);
        lvMeetings.Items.Add(lviItem);
        Marshal.ReleaseComObject(meeting);
    }
    Marshal.ReleaseComObject(currMail);
    Marshal.ReleaseComObject(restrictedItems);
}
Marshal.ReleaseComObject(calendarItems);
Marshal.ReleaseComObject(calendarFolder);
Marshal.ReleaseComObject(nameSpace);
}

Switch to the AddinModule designer and add a new ADXOutlookFormsManager.

Adding a new ADXOutlookFormsManager

Select the ADXOutlookFormsManager and click the ellipses (…) button next to its Items property. Add a new item to the Outook forms collection by clicking the Add button. Set the following properties:

  • Name: FormsCollectionEmailItem
  • InspectorAllowedDropregions: BottomSubpane;InspectorRegion;LeftSubpane;RightSubpane;TopSubpane
  • InspectorItemTypes: Mail
  • InspectorLayout: BottomSubPane
  • InspectorMode: Read
  • AlwaysShowHeader: True
  • DefaultRegionState: Minimized
  • FormClassName: OutlookInspectorRegionsDemo.EmailInfoForm
  • IsDragDropAllowed: True

Setting the InspectorAllowedDropRegions property to BottomSubpane;LeftSubpane;RightSubpane;TopSubpane will allow the user to drag and drop or dock the Add-in Express Outlook form on the bottom, top, left and right of the Outlook e-mail. Adding InspectorRegion to the list allows the user to drop the form in the “middle” of the form, completely replacing the standard Outlook form, but giving the user the option of switching between the two UIs. To see this in action, build and register the project.

When opening an e-mail message, you will see a list of all meetings organized by the sender.

A list of all Outlook meetings organized by the sender

Let’s add one more Inspector advanced region by adding another ADX Outlook form and creating a design similar to the screenshot below:

Adding one more Inspector advanced region

Next, we’ll add similar code that we’ve added above to the form’s Load event, but this time it will return a list of e-mails sent by the selected contact.

private void ContactInfoForm_Load(object sender, EventArgs e)
{
    Outlook._Application OutlookApp = this.OutlookAppObj as Outlook._Application;
    Outlook._Inspector currInspector = this.InspectorObj as Outlook._Inspector;
    Outlook._NameSpace nameSpace = OutlookApp.GetNamespace("MAPI");
    Outlook.MAPIFolder inboxFolder = nameSpace.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox);
    Outlook.Items inboxItems = inboxFolder.Items;
    string contactName = string.Empty;
 
    if (currInspector.CurrentItem != null && currInspector.CurrentItem is Outlook.ContactItem)
    {
        Outlook.ContactItem currContact = currInspector.CurrentItem as Outlook.ContactItem;
        contactName = currContact.FullName;
 
        string propertyTag = "https://schemas.microsoft.com/mapi/proptag/0x0042001f";
        string restriction = String.Format("@SQL=({0}{1}{0} LIKE '%{2}%')", (char)34, propertyTag, contactName);
        Outlook.Items filteredMailItems = inboxItems.Restrict(restriction);
 
        for (int i = 1; i <= filteredMailItems.Count; i++)
        {
            Outlook._MailItem email = filteredMailItems[i] as Outlook._MailItem;
            ListViewItem lviItem = new ListViewItem(email.Subject);
            ListViewItem.ListViewSubItem lvsiDateSent = new ListViewItem.ListViewSubItem(lviItem, email.SentOn.ToShortDateString());
            lviItem.SubItems.Add(lvsiDateSent);
 
            lvEmails.Items.Add(lviItem);
            Marshal.ReleaseComObject(email);
        }
        Marshal.ReleaseComObject(currContact);
        Marshal.ReleaseComObject(filteredMailItems);
    }
    Marshal.ReleaseComObject(inboxItems);
    Marshal.ReleaseComObject(inboxFolder);
    Marshal.ReleaseComObject(nameSpace);
}

Switch back to the AddinModule designer and click on the ellipses (…) button next to its Items property. Add a new item to the forms collection by clicking the Add button and set the following properties:

  • Name: FormsCollectionContactitem
  • InspectorItemTypes: Contact
  • InspectorLayout: LeftSubPane
  • AlwaysShowHeader: True
  • FormClassName: OutlookInspectorRegionsDemo.ContactInfoForm

In this case, we’re not allowing drag and drop, so the region will only show on the left hand side of the contact form. Build, register and run your project. If all went well you would be able to see all the e-mails send by a specific contact when opening a contact.

A form showing all e-mails send by a specific contact

That’s it for this week. Thank you for reading. Until next time, keep coding!

Available downloads:

This sample add-in was developed using Add-in Express 2010 for Office and .net:
Outlook Form Regions demo in C#

You may also be interested in:

2 Comments

  • Mike VE says:

    I can not make regions/pane work with Calendar view. The only ExplorerLayout settings that seem to work in Calendar are the Dock ones which are less than ideal for what I need. I can find no mention of this anywhere on the site.

  • Dmitry Kostochko (Add-in Express Team) says:

    Hi Mike,

    If I understand right, you are using Outlook 2010 or 2013. In these versions TopSubpane, BottomSubpane, RightSubpane, LeftSubpane and FolderView Explorer regions do not work in Calendar folders. We mention this among Known Issues in whatsnew.txt and on the Latest News page.

Post a comment

Have any questions? Ask us right now!