Introduction
This short article is a step by step tutorial on creating Form Regions and displaying it from a Ribbon. The technologies used in the sample code are Visual Studio 2008, C# 3.5 and Outlook 2007.
Creating a Form Region
To create a Form Region in Visual Studio 2008 right click on your Outlook Add In project, select Add then New Item. Select the Outlook Form Region template and name it “CustomEmailForm”. Click the Add button below to bring up the Form Region wizard. More information about Creating Outlook Form Regions.
We are going to use the Visual Studio 2008 designer to design our Form Region so select Design a new form region option then click, Next.
When I was researching about Form Regions last year, I seldom see samples that uses the Replace or Replace All option so in this example, we will choose Replace All. More information about the form types on Creating Outlook Form Regions.
For the text and display preferences let’s use the default for now. Click Next to continue to the next screen.
Let us call our custom message class as “IPM.Note.CustomEmailForm”. More information about message class at Associating a Form Region with an Outlook Message Class.
Designing the Form Region
For now we will just design a simple Form Region that doesn’t do anything. Our goal is to display it when we click the “Open Form Region” button in our Ribbon. The Form Region will look like this:
Creating a Ribbon
To create a Ribbon simply add a new item then select the Ribbon “Visual Designer” template. Rename the Ribbon group to Sample OBA. Drag and drop a button from the toolbox under the Office Ribbon Controls tab. Change its ControlSize property to RibbonControlSizeLarge and its label to Open Form Region. Let’s name this button buttonOpenFormRegion.
To be able to view our Add In we must specify in what window we want to display it. In this case we will choose the mail window specifically when creating and reading new email messages. To make that happen we have to specify the following Ribbon Types:
After setting up our Ribbon let’s do some coding. On the click event of buttonOpenFormRegion add the following code:
1
2 // Required namespaces
3 using Microsoft.Office.Tools.Ribbon;
4 using Microsoft.Office.Interop.Outlook;
5
6
7 private void buttonOpenFormRegion_Click(
8 object sender,
9 RibbonControlEventArgs e
10 )
11 {
12 Application application =
13 new ApplicationClass();
14 NameSpace ns =
15 application.GetNamespace("MAPI");
16 MAPIFolder mapi =
17 ns.GetDefaultFolder(OlDefaultFolders.olFolderNotes);
18
19 MailItem mailItem =
20 (MailItem)mapi.Items.Add("IPM.Note.CustomEmailForm");
21 mailItem.Display(false);
22 }
23
Earlier we defined our Form Region to have a custom message class called “IPM.Note.CustomEmailForm”. In order to open or display the Form Region we have to use the same message class name and add it to the MailItem object like in the sample code above.
Test and Run
Press F5 in Visual Studio to test in Outlook. Click New Mail Message in Outlook 2007. Notice that there is a new menu called “Add-Ins”. Click it and you will see the Ribbon that we made.
Click the button to see our Custom Email Form in action.
That's it. You can download the source code here.
Related article: