Ways to Customize Outlook
Outlook has an object model that consists of 67 objects that combined have more than 1,700 properties and methods. The Outlook object model is about a third as big as the Excel and Word object models and tends to give you less control over Outlook than you would expect. Outlook does have a larger number of events compared to the Word and Excel object modelsmore than 300 events. However, the large number of events is mainly due to 16 events that are duplicated on 15 Outlook objects.The main way that you will integrate your code into Outlook is via add-ins. This model was originally designed to allow the integration of COM components written in VB6, VBA, C, or C++. However, through COM interop, a managed object can masquerade as a COM object and participate in the Outlook add-in model.
Automation Executable
As mentioned in Chapter 2, "Introduction to Office Solutions," you can start Outlook from a console application or Windows application and automate it from that external program. The problem with this approach is that you cannot add your automation executable to the exclusion list of the Outlook object model security guard.Chapter 11. It does not have a mechanism to trust an automation executable and let an automation executable bypass the guard.
Add-Ins
When building add-ins for Outlook, you have two choices: You can either build a COM add-in or a VSTO Outlook add-in. A VSTO Outlook add-in solves many of the problems associated with COM add-in development and is the preferred model for Outlook 2003 add-in development. You can read about this model for Outlook add-ins in Chapter 24, "Creating Outlook Add-Ins with VSTO." The only time you would want to consider building a COM add-in instead is if you need to target versions of Outlook that are older than Outlook 2003. You can read about building COM add-ins in Chapter 23, "Developing COM Add-Ins for Word and Excel."Outlook has a COM add-ins dialog box that enables users to enable and disable add-ins. Both VSTO add-ins and COM add-ins appear in the COM Add-Ins dialog box. This dialog box is very well hidden. To access the COM Add-Ins dialog, you must follow these steps:
1. | Choose Options from the Tools menu to bring up the Options dialog. |
2. | Click the Other tab of the Options dialog. |
3. | Click the Advanced Options button to bring up the Advanced Options dialog. |
4. | Click the COM Add-Ins button to bring up the COM Add-Ins dialog. |
Figure 9-1 shows the COM Add-Ins dialog.
Figure 9-1. The COM Add-Ins dialog in Outlook.

Smart Tags
Smart Tags are a feature that enables the display of a pop-up menu with actions for a given piece of text on the screen. Outlook supports Smart Tags in several ways.
Smart Tags When Word Is the E-mail Editor
First, if Word is used as the e-mail editor in Outlook, Smart Tags appear when you edit e-mail messages. To set Word as the e-mail editor, you can use the Options command from the Tools menu to display Outlook's Options dialog. On the Mail Format page, check the Use Microsoft Office Word 2003 to edit e-mail messages, as shown in Figure 9-2.
Figure 9-2. Specifying Word as the e-mail editor.

Figure 9-3. Smart Tags in an e-mail message when Word is the e-mail editor.
[View full size image]

Smart Tags in the Research Task Pane When Word Is the E-mail Editor
You can register Smart Tags to recognize text in the Research task pane. If Word is being used as the e-mail editor, the user can select some text in the e-mail, right-click the text, and choose Look Up to bring up the Research task pane. Results in the Research task pane may include text that is tagged by Smart Tags.
Smart Tags Embedded i201 Format E-mail and Displayed in the Reading Pane
A third way Smart Tags are supported in Outlook is if you use Word as the e-mail editor and send e-mail i201 format. If Word's send format is configured appropriately, Smart Tags can be embedded in th192 formatted message. Users who read the messages that have the Smart Tag installed and have Outlook's security settings set to allow it will be able to see Smart Tags display in Outlook's reading pane. Outlook's reading pane is effectively a201 Web browser.Figure 9-4. In the General page of this dialog, you must set th192 filtering options to None or Medium and check the Save Smart Tags in e-mail check box.
Figure 9-4. E-mail options to enable the embedding of Smart Tags i201 e-mail messages.

Figure 9-5. Internet zone security required to allow Smart Tags to be displayed in e-mail messages.

Figure 9-6. A Smart Tag displayed in the reading pane.
[View full size image]

Persona Menu Smart Tags
The final way Smart Tags are supported in Outlook is via the Persona menu. This menu appears on e-mail items and other Outlook items when you click the Persona icon shown in many Outlook views. Figure 9-7 shows the Persona icon and the menu that appears when you click it. Smart Tag actions appear in the Additional Actions submenu that is shown in Figure 9-7.
Figure 9-7. The Persona menu in OutlookSmart Tag actions appear under the Additional Actions submenu.

Custom Property Pages
An Outlook add-in can add a custom property page to the Properties dialog box for a folder or to Outlook's Options dialog. We walk through how this is done using a VSTO Outlook add-in. First, create a VSTO Outlook add-in project in VSTO by following the instructions in Chapter 24.After you have created a basic VSTO Outlook add-in project, you need to add a user control project item to the project. A user control is a special kind of Windows Forms control that is useful for inserting into another window. To add a user control to your project, click the project node in the Solution Explorer, and then choose Add User Control from the Project menu. When you double-click the newly added user control project item, you will see the user control designer shown in Figure 9-8. You can resize the user control using the drag handle in the lower-right corner. Resize it to about 410 x 355 pixels, which is the size of a property page in Outlook. With the user control resized, use the controls toolbox (choose Toolbox from the View menu if it is not already showing) to add controls to your user control surface. In Figure 9-8, we have added several check boxes, radio buttons, and buttons to the user control surface.
Figure 9-8. The user control designer.

Listing 9-1. First Version of the Modified User Control Class
With the user control created, two event handlers must be added. The first event handler is for the Application object's OptionsPagesAdd event. This event is raised when Outlook is ready to add custom property pages to the Outlook Options dialog, which is shown when the user chooses Options from the Tools menu. The event handler is passed a pages parameter of type PropertyPages that has an Add method that can be used to add a user control as a custom property page.The second event handler is for the NameSpace object's OptionsPagesAdd event. This event is raised when Outlook is ready to add custom property pages when a properties dialog box for a folder is displayed. The properties dialog box for a folder is shown when the user right-clicks a folder and chooses Properties from the pop-up menu. The event handler is passed a pages parameter of type PropertyPages that has an Add method that can be used to add a user control as a custom property page. The event handler is also passed a folder parameter of type MAPIFolder that specifies the folder for which the properties dialog box will be shown.Listing 9-2 shows an implementation of a VSTO ThisApplication class that handles these two events. In the event handlers for the Application object's OptionsPagesAdd event and the NameSpace object's OptionsPagesAdd event, an instance of the user control in Listing 9-1 is created and passed as the first parameter to the PropertyPages.Add method. The second property is passed an empty string because the caption for the custom property page is retrieved by Outlook calling the PageCaption property on the user control that has been attributed with a DispID known to Outlook.
using System;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Runtime.InteropServices;
namespace OutlookAddin1
{
public partial class UserControl1 :
UserControl, Outlook.PropertyPage
{
const int captionDispID = -518;
bool isDirty = false;
public UserControl1()
{
InitializeComponent();
}
void Outlook.PropertyPage.Apply()
{
MessageBox.Show("The user clicked the Apply button.");
}
bool Outlook.PropertyPage.Dirty
{
get
{
return isDirty;
}
}
void Outlook.PropertyPage.GetPageInfo(ref string helpFile,
ref int helpContext)
{
}
[DispId(captionDispID)]
public string PageCaption
{
get
{
return "Test Page";
}
}
}
}
Listing 9-2. A VSTO Outlook Add-In That Handles the OptionsPagesAdd Event on Application and Namespace
If you compile and run this VSTO add-in, you will get the result shown in Figure 9-9 when you show Outlook's Options dialog and click the Test Page tab.
using System;
using System.Windows.Forms;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace OutlookAddin1
{
public partial class ThisApplication
{ Outlook.NameSpace nameSpace;
private void ThisApplication_Startup(object sender, EventArgs e)
{
this.OptionsPageAdd += new
Outlook.ApplicationEvents_11_OptionsPagesAddEventHandler(
ThisApplication_OptionsPagesAdd);
nameSpace = this.Session;
nameSpace.OptionsPagesAdd += new
Outlook.NameSpaceEvents_OptionsPagesAddEventHandler(
NameSpace_OptionsPagesAdd);
}
private void ThisApplication_Shutdown(object sender, EventArgs e)
{
}
void ThisApplication_OptionsPagesAdd(Outlook.PropertyPages pages)
{
pages.Add(new UserControl1(), ");
}
void NameSpace_OptionsPagesAdd(Outlook.PropertyPages pages,
Outlook.MAPIFolder folder)
{
pages.Add(new UserControl1(), ");
}
#region VSTO Designer generated code
private void InternalStartup()
{
this.Startup += new EventHandler(ThisApplication_Startup);
this.Shutdown += new EventHandler(ThisApplication_Shutdown);
}
#endregion
}
}
Figure 9-9. A custom property page added to Outlook's Options dialog.

Figure 9-10. A custom property page added to a folder's Properties dialog.

Listing 9-3. Second Version of a User Control Class That Handles Dirty State Properly
Now when you run the add-in and change the checked state of the first check box in the custom property page, the dirty state is changed and Outlook's PropertyPage-Site is notified. The result is that the Apply button is enabled. Clicking the Apply button invokes the test dialog in Listing 9-3's implementation of the Apply method.
using System;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Runtime.InteropServices;
namespace OutlookAddin1
{
public partial class UserControl1 : UserControl,
Outlook.PropertyPage
{
const int captionDispID = -518;
bool isDirty = false;
private Outlook.PropertyPageSite propertyPageSite = null;
public UserControl1()
{
InitializeComponent();
this.Load += new EventHandler(UserControl1_Load);
this.checkBox1.CheckedChanged += new
System.EventHandler(this.OnCheckBox1Changed);
}
void Outlook.PropertyPage.Apply()
{
MessageBox.Show("The user clicked the Apply button.");
}
bool Outlook.PropertyPage.Dirty
{
get
{
return isDirty;
}
}
void Outlook.PropertyPage.GetPageInfo(ref string helpFile,
ref int helpContext) { }
[DispId(captionDispID)]
public string Caption
{
get
{
return "Test Page";
}
}
private void SetIsDirty(bool value)
{
isDirty = value;
propertyPageSite.OnStatusChange();
}
private void OnCheckBox1Changed(object sender, EventArgs e)
{
SetIsDirty(true);
}
void UserControl1_Load(object sender, EventArgs e)
{
InitializePropertyPageSite();
}
void InitializePropertyPageSite()
{
string windowsFormsStrongName =
typeof(System.Windows.Forms.Form).Assembly.FullName;
Type oleObjectType = Type.GetType(
System.Reflection.Assembly.CreateQualifiedName(
windowsFormsStrongName,
"System.Windows.Forms.UnsafeNativeMethods")).
GetNestedType("IOleObject");
System.Reflection.MethodInfo getClientSiteMethodInfo =
oleObjectType.GetMethod("GetClientSite");
propertyPageSite = (Outlook.PropertyPageSite)
getClientSiteMethodInfo.Invoke(this, null);
}
}
}