Free Trial

Safari Books Online is a digital library providing on-demand subscription access to thousands of learning resources.

  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint
Share this Page URL

Chapter 16. Working with Outlook Form Re... > Outlook Form Region Programmability

Outlook Form Region Programmability

In this section, we examine in more detail the Outlook form region classes that VSTO creates. As with many other VSTO project items, it uses partial classes to display user code (code edited by the developer of the add-in) and associate it with generated code (code generated by Visual Studio) to build the final Outlook form region class.

The VSTO Form Region Class

In the example from the first section of this chapter, right-clicking FormRegion1.cs and choosing View Code from the context menu shows the user code:

partial class FormRegion1

You can also see the generated code by expanding FormRegion1.cs and double-clicking the file FormRegion1.Designer.cs, which is a child of FormRegion1.cs. Here, you see this line of code, showing that a VSTO form region class derives from Microsoft.Office.Tools.Outlook.FormRegionControl:

partial class FormRegion1 :

Then, if you look at the definition of FormRegionControl, you see that it derives from System.Windows.Forms.UserControl. A form region class is primarily a Windows Forms UserControl with extensions like the implementation of the IFormRegion interface, which is used by VSTO to start and shut down a form region:

public class FormRegionControl : UserControl, IFormRegion

When you import an .OFS file, the form region class derives from Microsoft.Office.Tools.Outlook.ImportedFormRegion.

The Form Region Factory

The form region factory is mostly an internal implementation detail of how VSTO supports Outlook form regions. You can do some advanced things with custom form region factories, such as having your form region classes in a separate assembly from the add-in or having a single factory to handle multiple form region classes. But outside these advanced scenarios, the form region factory does peek through in one significant way: It exposes the FormRegionInitializing method, which can be handled in your code to prevent a form region from being displayed for a particular Outlook item based on criteria you set. As you might expect, the Factory object creates an instance of your form region class every time an Outlook item requires a form region to be displayed. If you have an Adjacent form region that displays in the reading pane for a list of mail items, for example, each time the selection changes to a different mail item, a new instance of your form region class is created for the current mail item. The factory object is invoked first, and if the FormRegionInitializing method doesn’t cancel the creation of the form region by the implementation of the method setting the Cancel property of the e parameter to true, a new instance of the form region class is created.

When you import an .OFS file, the form region factory also has a method called InitializeManifest in which you can write code to modify settings for the form region, such as the form region type. With Windows Forms-based form regions, you typically modify these form region settings in the Properties window, and no InitializeManifest method is in the form region factory.

Another key element of the form region factory class is the FormRegionMessageClass attribute, which sets the message classes—both built-in and custom—the form region will be displayed for. Listing 16-6 shows the attributes of a form region factory class associated with three built-in message classes (Appointment, Contact, and Note) and one custom message class that derives from Task (IPM.Task.Foo). VSTO provides constant strings in the Microsoft.Office.Tools.Outlook.FormRegionMessageClassAttribute namespace for each of the built-in Outlook message classes. If you want, you can interchange the constant string. The string "IPM.Task", for example, is equivalent to the Microsoft.Office.Tools.Outlook.FormRegionMessageClassAttribute.Task constant.

Listing 16-6. A Form Region Factory Class Associated with Three Built-In Message Classes and One Custom Message Class via FormRegionMessageClass Attributes

public partial class FormRegion3Factory

The Manifest Object

Most of the properties that control how a VSTO form region is displayed by Outlook are found in the FormRegionManifest object returned by the Manifest property of a VSTO form region. Behind the scenes, setting properties on the FormRegionManifest object manipulates an XML manifest that describes the form region. This manifest is provided to Outlook when the add-in loads. You can modify the properties of the FormRegionManifest object via the property grid for Windows Forms-based form regions by clicking the form region surface in the designer and then using the Properties window to set properties associated with the Manifest property, as shown in Figure 16-30.

Figure 16-30. Setting properties on the manifest object for a Windows Forms-based form region.

To set properties of the manifest object for a Outlook Forms-based form region, write code in the InitializeManifest method of the form region factory. If you try to set properties on the manifest object outside these two mechanisms (the Properties window for Windows Forms and the InitializeManifest method for Outlook Forms), chances are that Outlook will already have asked for the manifest object’s settings, and your code will generate an InvalidOperationException. You can use the Locked property of the manifest object to check whether Outlook has already retrieved the settings from the manifest object. If Locked is set to true, any code you write against the manifest object will have no effect.

Table 16-4 describes the various properties of the manifest object. The table refers several times to the Choose Form dialog box, which you invoke by dropping down the New button in the Explorer window and choosing Choose Form, as shown in Figure 16-31. The dialog box shown in Figure 16-32 appears, allowing you to pick Replacement or Replace-All form regions (which are associated with custom message classes). This way, an end user can create an Outlook item with a custom message class that you defined and associated with a form region. Table 16-5 describes the icons that can be used by a form region.

Figure 16-31. Creating a new Outlook Item by using the Choose Form button.

Figure 16-32. The Choose Form dialog box.

Table 16-4. Properties of the Manifest Object
NameTypeWhat It Does
ContactstringGets and sets the name used in the Choose Form dialog box for Replacement and Replace-All form regions.
CustomActionsFormRegionCustomActionCollectionA collection with custom actions associated with the form region. The custom actions appear in a Custom Actions group in the Ribbon of the Inspector window showing the form region.
DescriptionstringGets and sets the description used in the Choose Form dialog box for Replacement and Replace-All form regions.
DisplayAfterstringGets or sets the name of the form region to display before this form region.
ExactMessageClassboolIf set to true, this property prevents a custom message class derived from the message class for the form region from displaying the form region.
FormRegionNamestringGets and sets the name used for the Ribbon button associated with this form region or the header associated with an Adjoining form region.
FormRegionTypeFormRegionTypeGets and sets an enum specifying the form region type: Adjoining, Replacement, Replace-All, or Separate.
HiddenboolIf set to true, this form region won’t be displayed in the Choose Form dialog box.
IconsFormRegionManifestIconsSets the icons used by the form region (see Table 16-5).
LockedboolReturns true if Outlook has already queried the manifest object for its settings. Any code you write against the manifest object after Locked is set to true has no effect.
RibbonAcceleratorstringGets and sets the keyboard shortcuts for Separate, Replacement, and Replace-All form regions.
ShowInspectorComposeboolGets and sets whether the form region is shown when an Inspector window is in compose mode.
ShowInspectorReadboolGets and sets whether the form region is shown when an Inspector window is in read mode.
ShowReadingPaneboolGets and sets whether a form region is shown for the reading pane.
TitlestringGets and sets the name that appears in the Actions menu and the Choose Form dialog box for Replacement and Replace-All form regions.


Table 16-5. Icons on the Manifest Object
NameWhat It DoesApplies to Form Region Types
Default16 × 16-pixel icon that is used by default.Replacement and Replace-All
Encrypted16 × 16-pixel icon for encrypted items.Replacement and Replace-All
Forwarded16 × 16-pixel icon for forwarded items.Replacement and Replace-All
PageIcon used in the Ribbon of an Inspector window for the button that activates the form region. Use a PNG file for this icon.Separate, Replacement, and Replace-All
Read16 × 16-pixel icon for read items.Replacement and Replace-All
Recurring16 × 16-pixel icon for recurring items.Replacement and Replace-All
Replied16 × 16-pixel icon for replied-to items.Replacement and Replace-All
Signed16 × 16-pixel icon for digitally signed items.Replacement and Replace-All
Submitted16 × 16-pixel icon for items in the Outbox that are submitted for sending.Replacement and Replace-All
Unread16 × 16-pixel icon for unread items.Replacement and Replace-All
Unsent16 × 16-pixel icon for items in the Drafts folder that are not yet sent.Replacement and Replace-All
WindowAppears in the notification area and in the Alt+Tab window for Inspector windows displaying the form region. Use a 32 × 32-pixel icon.Replacement and Replace-All

Other Key Properties and Methods

Several other properties and methods associated with a Outlook form region class created with VSTO are worth pointing out. We’ve already talked about the OutlookItem property, which returns as an object the Outlook Item associated with the Outlook form region. You can cast the object returned by the OutlookItem property to the type of Outlook item you expect, based on what built-in or custom message classes your form region is associated with.

The OutlookFormRegion property returns the underlying Microsoft.Office.Interop.Outlook.FormRegion object, which represents your form region in the Outlook object model. Table 16-6 shows some of the key properties and methods on this object.

Table 16-6. Key Properties and Methods on the FormRegion Object Returned by the OutlookFormRegion Property
NameTypeWhat It Does
DetailstringGets and sets the name displayed in the header after the display name of an Adjoining form region.
InspectorInspectorReturns the Inspector window object associated with the form region.
IsExpandedboolRead-only property that returns true when an Adjoining form region is expanded.
LanguageintReturns the locale ID (LCID) for the current language used by Outlook.
Reflow() Method that forces Outlook to size an Adjoining form region so that all controls are visible.
Select() Makes the form region the active form region and forces it to be visible.

Globals Support

Whenever you create a form region in a VSTO project, it is added to the Globals object for the project. You can access the currently active form regions in three ways: You can see all the active form regions for a particular Inspector window, for a particular Explorer window, or for all open windows. You can access only form regions provided by your add-in; you can’t access form regions provided by other add-ins by using the Globals object.

Listing 16-7 shows a subroutine that uses all three ways of accessing active form regions to get the count of active form regions associated with the active Explorer window, the count of active form regions associated with the active Inspector window, and the total count of all active form regions.

Listing 16-7. Three Methods of Accessing Active Form Regions: All Active, for an Explorer Window, and for an Inspector Window

private void ShowActiveFormRegions()
  Outlook.Explorer explorer =
  Outlook.Inspector inspector =

    String.Format("{0} total form regions",

  if (explorer != null)
      String.Format("{0} for regions for the active Explorer",

  if (inspector != null)
      String.Format("{0} for regions for the active Inspector",

  • Safari Books Online
  • Create BookmarkCreate Bookmark
  • Create Note or TagCreate Note or Tag
  • PrintPrint