Creating an Outlook Add-In in VSTO
To create an Outlook add-in in VSTO, choose Project from the New menu of the File menu. The Outlook add-in project appears in the list of templates under the Visual C#/Office node in the tree of project types, as shown in Figure 24-6. Type a name for your new Outlook add-in project and pick a location for the project. Then click the OK button.
Figure 24-6. Creating a new Outlook add-in project.
[View full size image]

Figure 24-7. The Outlook add-in project in Solution Explorer.

Listing 24-2. ThisApplication.cs for an Outlook Add-In Project
When you run the project with the code shown in Listing 24-2, Outlook is launched and the add-in loads and displays a dialog box showing the count of the Inspectors and Explorers. Now go to Outlook's COM Add-In dialog by following these steps:
using System;
using System.Windows.Forms;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Office = Microsoft.Office.Core;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace OutlookAddin1
{
public partial class ThisApplication
{
private void ThisApplication_Startup(object sender, EventArgs e)
{
MessageBox.Show(String.Format(
"There are {0} inspectors and {1} explorers open.",
this.Inspectors.Count, this.Explorers.Count));
}
private void ThisApplication_Shutdown(object sender, EventArgs e)
{
MessageBox("Goodbye");
}
#region VSTO Designer generated code
private void InternalStartup()
{
this.Startup += new EventHandler(ThisApplication_Startup);
this.Shutdown += new EventHandler(ThisApplication_Shutdown);
}
#endregion
}
}
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 24-8 shows the COM Add-Ins dialog. The add-in you just created (OutlookAddin1) is displayed as if it were a COM add-in. If you look at the location of the add-in, it claims to be in the C:\program files\Common Files\Microsoft Shared\VSTO\8.0 directory.
Figure 24-8. The COM Add-Ins dialog shows the VSTO Outlook add-in.

Figure 24-9. A VSTO Outlook Add-in registered under the Outlook Addins subkey.
[View full size image]

Figure 24-10. The InProcServer32 under the CLSID key associated with ProgID OutlookAddin1.
[View full size image]

Listing 24-3. The OutlookAddin1.manifest File
[View full width]
The manifest indicates that the actual managed add-in assembly that AddinLoader.dll will load is called OutlookAddin1.dll. The path provided in codebase will be relative to the location of the manifest (specified in ManifestLocation). So looking at the ManifestLocation key in Figure 24-10, we can see that the VSTO runtime will load OutlookAddin1.dll from the full path below:C:\Visual Studio Projects\OutlookAddin1\OutlookAddin1\bin\debug\OutlookAddin1.dll
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" xmlns:asmv2="urn:schemas-microsoft-com:asm.v2" manifestVersion="1.0">
<assemblyIdentity name="OutlookAddin1.manifest" version="1.0.0.0" />
<asmv2:entryPoint name="Startup" dependencyName="dependency0">
<asmv2:clrClassInvocation />
</asmv2:entryPoint>
<asmv2:dependency asmv2:name="dependency0">
<asmv2:dependentAssembly>
<assemblyIdentity name="OutlookAddin1" version="1.0.0.0" culture="neutral" />
</asmv2:dependentAssembly>
<asmv2:installFrom codebase="OutlookAddin1.dll" />
</asmv2:dependency>
</assembly>
Security
VSTO Outlook add-ins use the same security model that Word and Excel VSTO customizations use. That is, no Outlook add-in runs without .NET Framework security policy that trusts the Outlook add-in assembly and any dependent assemblies. When you create a new Outlook add-in project, Visual Studio automatically adds this policy to trust the bin directory for the project and any referenced assemblies that are copied locally to the project directory. When you deploy an Outlook add-in, however, you need to also create and install .NET policy that will trust the assemblies that are part of the Outlook add-in. Chapters 19, ".NET Code Security," and 20, "Deployment," cover this in more detail.The VSTO security model is also the key to how the "Trust all installed templates and add-ins" problem is solved. When this check box in the Macro Security dialog is unchecked, Office requires the InProcServer32 registered for the add-in to be signed. Because VSTO's security model is that no add-in runs without .NET Framework security policy to trust it, VSTO can sign the AddinLoader.dll because it will only load code that has been trusted by .NET Framework security policy. This makes it so that your add-in will load even in environments where this check box is not checked.
Manifest Updating
VSTO Outlook add-ins use the same basic updating and publishing mechanism that Word and Excel VSTO customizations use to update the manifest in a document. You can publish a VSTO Outlook add-in that embeds in the manifest a URL to a deploy manifest. To publish an add-in, right-click the project node in Solution Explorer and choose Publish from the pop-up menu. The Publish Wizard shown in Figure 24-11 will appear. Here we choose to publish to a local directory called c:\myaddins.
Figure 24-11. Publishing a VSTO Outlook add-in.

Installing
VSTO Outlook add-ins differ in one important way from Word and Excel VSTO customizations: They must be registered in the registry. This means that you will have to have an installer that installs your add-in onto a user's machine and puts the needed registry keys in the registry.Chapter 20.
Other VSTO Features
Although it would be nice, Outlook add-ins do not support VSTO's Smart Tags or ActionsPane features that are available to Word and Excel customizations. It also does not support the cached data feature of VSTO.