- Introduction to Form Regions
- Form Region Types and Custom Message Classes
- Creating an Outlook Forms-Based Form Region
- Outlook Form Region Programmability
- Conclusion
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 Form-Region1.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 : Microsoft.Office.Tools.Outlook.FormRegionControl
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 Initial-izeManifest method is in the form region factory.
Another key element of the form region factory class is the Form-RegionMessageClass 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
[Microsoft.Office.Tools.Outlook.FormRegionMessageClass( Microsoft.Office.Tools.Outlook. FormRegionMessageClassAttribute.Appointment)] [Microsoft.Office.Tools.Outlook.FormRegionMessageClass( Microsoft.Office.Tools.Outlook. FormRegionMessageClassAttribute.Contact)] [Microsoft.Office.Tools.Outlook.FormRegionMessageClass( Microsoft.Office.Tools.Outlook. FormRegionMessageClassAttribute.Note)] [Microsoft.Office.Tools.Outlook.FormRegionMessageClass( "IPM.Task.Foo")] [Microsoft.Office.Tools.Outlook.FormRegionName( "OutlookAddIn2.FormRegion3")] 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.
Table 16-4. Properties of the Manifest Object
Name |
Type |
What It Does |
Contact |
string |
Gets and sets the name used in the Choose Form dialog box for Replacement and Replace-All form regions. |
CustomActions |
FormRegionCustomAction-Collection |
A 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. |
Description |
string |
Gets and sets the description used in the Choose Form dialog box for Replacement and Replace-All form regions. |
DisplayAfter |
string |
Gets or sets the name of the form region to display before this form region. |
ExactMessage-Class |
bool |
If set to true, this property prevents a custom message class derived from the message class for the form region from displaying the form region. |
Form-RegionName |
string |
Gets and sets the name used for the Ribbon button associated with this form region or the header associated with an Adjoining form region. |
FormRegionType |
FormRegion-Type |
Gets and sets an enum specifying the form region type: Adjoining, Replacement, Replace-All, or Separate. |
Hidden |
bool |
If set to true, this form region won’t be displayed in the Choose Form dialog box. |
Icons |
FormRegionManifestIcons |
Sets the icons used by the form region (see Table 16-5). |
Locked |
bool |
Returns 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. |
Ribbon-Accelerator |
string |
Gets and sets the keyboard shortcuts for Separate, Replacement, and Replace-All form regions. |
ShowInspector-Compose |
bool |
Gets and sets whether the form region is shown when an Inspector window is in compose mode. |
ShowInspector-Read |
bool |
Gets and sets whether the form region is shown when an Inspector window is in read mode. |
ShowReading-Pane |
bool |
Gets and sets whether a form region is shown for the reading pane. |
Title |
string |
Gets and sets the name that appears in the Actions menu and the Choose Form dialog box for Replacement and Replace-All form regions. |
Figure 16-31 Creating a new Outlook Item by using the Choose Form button.
Figure 16-32 The Choose Form dialog box.
Table 16-5. Icons on the Manifest Object
Name |
What It Does |
Applies to Form Region Types |
Default |
16 × 16-pixel icon that is used by default. |
Replacement and Replace-All |
Encrypted |
16 × 16-pixel icon for encrypted items. |
Replacement and Replace-All |
Forwarded |
16 × 16-pixel icon for forwarded items. |
Replacement and Replace-All |
Page |
Icon 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 |
Read |
16 × 16-pixel icon for read items. |
Replacement and Replace-All |
Recurring |
16 × 16-pixel icon for recurring items. |
Replacement and Replace-All |
Replied |
16 × 16-pixel icon for replied-to items. |
Replacement and Replace-All |
Signed |
16 × 16-pixel icon for digitally signed items. |
Replacement and Replace-All |
Submitted |
16 × 16-pixel icon for items in the Outbox that are submitted for sending. |
Replacement and Replace-All |
Unread |
16 × 16-pixel icon for unread items. |
Replacement and Replace-All |
Unsent |
16 × 16-pixel icon for items in the Drafts folder that are not yet sent. |
Replacement and Replace-All |
Window |
Appears 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
Name |
Type |
What It Does |
Detail |
string |
Gets and sets the name displayed in the header after the display name of an Adjoining form region. |
Inspector |
Inspector |
Returns the Inspector window object associated with the form region. |
IsExpanded |
bool |
Read-only property that returns true when an Adjoining form region is expanded. |
Language |
int |
Returns 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 = Globals.ThisAddIn.Application.ActiveExplorer(); Outlook.Inspector inspector = Globals.ThisAddIn.Application.ActiveInspector(); System.Windows.Forms.MessageBox.Show( String.Format("{0} total form regions", Globals.FormRegions.Count.ToString())); if (explorer != null) System.Windows.Forms.MessageBox.Show( String.Format("{0} for regions for the active Explorer", Globals.FormRegions[explorer].Count.ToString())); if (inspector != null) System.Windows.Forms.MessageBox.Show( String.Format("{0} for regions for the active Inspector", Globals.FormRegions[inspector].Count.ToString())); }