Creating an Outlook AddIn in VSTO


To create an Outlook add-in in VSTO, choose File > New > Project. The Outlook add-in project appears in the list of templates under the Visual Basic/Office node in the tree of project types, as shown in Figure 24.6. Type a name for your new Outlook add-in project; pick a location for the project; then click the OK button.

Figure 24.6. Creating a new Outlook add-in project.


A project is created with references to the Outlook 2003 primary interop assembly (PIA), the core Office PIA, and other needed references, as shown in Figure 24.7. One project item, called ThisApplication.vb, is created.

Figure 24.7. The Outlook add-in project in Solution Explorer.


If you doubleclick the ThisApplication.vb project item, you will see a simple code view, shown in Listing 24.2, that looks very similar to the ThisDocument.vb project item in the Word document or template VSTO project, and to the Sheet1.vb project item in the Excel workbook or template project. There is a simple Startup and Shutdown method where you can write code that executes on the startup and shutdown of the add-in. Startup is roughly the equivalent of OnConnection in IDTExtensibility2based add-ins, and Shutdown is roughly the equivalent of OnDisconnection. Listing 24.2 also illustrates that the ThisApplication class derives from an aggregate of the Outlook Application object, enabling you to access properties and methods of the Outlook Application object by writing code such as Me.Inspectors.Count.

Listing 24.2. ThisApplication.vb for an Outlook AddIn Project

Imports Office = Microsoft.Office.Core Imports Outlook = Microsoft.Office.Interop.Outlook Public Class ThisApplication   Private Sub ThisApplication_Startup(ByVal sender As Object, _     ByVal e As System.EventArgs) Handles Me.Startup     MsgBox(String.Format( _       "There are {0} inspectors and {1} explorers open.", _       Me.Inspectors.Count, Me.Explorers.Count))   End Sub   Private Sub ThisApplication_Shutdown(ByVal sender As Object, _     ByVal e As System.EventArgs) Handles Me.Shutdown     MsgBox("Goodbye")   End Sub End Class 


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 AddIns dialog box by following these steps:

1.

Choose Options from the Tools menu to bring up the Options dialog box.

2.

Click the Other tab of the Options dialog box.

3.

Click the Advanced Options button to bring up the Advanced Options dialog box.

4.

Click the COM AddIns button to bring up the COM AddIns dialog box.

Figure 24.8 shows the COM AddIns dialog box. The add-in you just created (OutlookAddin1) is displayed as though 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 AddIns dialog box shows the VSTO Outlook add-in.


From the standpoint of Outlook, Outlook believes that it is loading a COM add-in, even though we know this is a VSTO Outlook add-in project. What is going on here? To answer that, let's do a little digging in the registry to understand how VSTO is connecting everything. If we look under HKEY_CURRENT_USER\Software\Microsoft\Office\Outlook\Addins, we will find a registry key for the add-in we created in VSTO, called OutlookAddin1 in our example, as shown in Figure 24.9. The registry entries look just like those for an IDTExtensibility2 add-in, as described in Chapter 23, "Developing COM AddIns for Word and Excel." These registry entries make Outlook think that it is just loading a COM add-in.

Figure 24.9. A VSTO Outlook add-in registered under the Outlook Addins subkey.


If we search the registry under the HKEY_CLASSES_ROOT\CLSID key for the ProgID OutlookAddin1, we will find a key associated with the OutlookAddin1 ProgID. Looking under the InprocServer32 key for that ProgID, we see the entries in Figure 24.10.

Figure 24.10. The InprocServer32 under the CLSID key associated with ProgID OutlookAddin1.


Under the InprocServer32 key are several important values. First, the (Default) value is the DLL that Outlook will start to load the VSTO Outlook add-in we created. The DLL name is AddinLoader.dll. This is a VSTOprovided replacement for mscoree.dll that can load a managed add-in without the problems associated with mscoree.dll listed at the start of this chapter. This DLL also solves the Outlook shutdown problem, making it so that your add-in will always shut down cleanly and not leave Outlook running.

Second, we see a ManifestLocation key. Because the VSTO Outlook add-in project uses the VSTO runtime to load the add-in, a manifest is required to specify what to load. This manifest is identical to the manifest embedded in VSTO customized Word documents and Excel spreadsheets. The name of the manifest is stored in the ManifestName key. If we go to the ManifestLocation and open the file with the name ManifestName (OutlookAddin1.manifest), we will see the XML shown in Listing 24.3.

Listing 24.3. The OutlookAddin1.manifest File

<assembly xmlns="urn:schemasmicrosoftcom:asm.v1" xmlns:asmv2="urn:schemasmicrosoftcom: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> 


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

Security

VSTO Outlook add-ins use the same security model that Word and Excel VSTO customizations usethat 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 also need to create and install .NET policy that will trust the assemblies that are part of the Outlook add-in. Chapter 19, ".NET Code Security," and Chapter 20, "Deployment," cover this topic in more detail.

The VSTO security model is also the key to how the Trust All Installed AddIns and Templates problem is solved. When this check box in the Security dialog box 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 load only 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, rightclick the project node in Solution Explorer, and choose Publish from the popup 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.


This causes a manifest to be generated that is slightly different from the manifest in Listing 24.3 earlier in this chapter. The first difference is that now the manifest points to a deploy manifest. Each time an Outlook add-in that has been published and that has a deploy manifest location is loaded, the deploy manifest is checked to see whether a newer version of the manifest is available. If there is, a new version of the manifest is pulled down to the ManifestLocation specified in the registry, and it overwrites the old manifest. The second difference is that DLLs referred to in the application manifest are now located relative to the path to the deploy manifest instead of the application manifest. For more information on publishing and deploy manifests, see Chapter 20, "Deployment."

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.

When you create a VSTO Outlook add-in project, a setup project for the add-in is created for you automatically. This setup project will generate an installer that puts the required registry keys in the registry and copies the manifest and add-in DLL to the desired location. It does not install the VSTO runtime redistributable (vstor.exe) or configure .NET security policy to trust the add-in. These steps must either be added manually to the setup project or performed as a separate step when rolling out VSTO to an enterprise. For more information, see Chapter 20, "Deployment."

Other VSTO Features

Although it would be nice, Outlook add-ins do not support VSTO's Smart Tags or ActionsPane features, which are available to Word and Excel customizations. They also do not support the cacheddata feature of VSTO.




Visual Studio Tools for Office(c) Using Visual Basic 2005 with Excel, Word, Outlook, and InfoPath
Visual Studio Tools for Office: Using Visual Basic 2005 with Excel, Word, Outlook, and InfoPath
ISBN: 0321411757
EAN: 2147483647
Year: N/A
Pages: 221

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net