Writing an Outlook Add-in with C# (Part 3)
To this point we have an Outlook Add-in shell written. We have verified that Outlook sees it and that Outlook loads it when it starts up. (See Part 1 and Part 2 if you missed them). Now we are going to make it do something interesting inside Outlook.
To start with, let’s add a tab to the Outlook dialog when someone brings up a Contact.
First, you have to add a reference to the Outlook COM object so the C# compiler knows what Outlook is. Open your MyAddin project in Dev Studio and choose Project/Add Reference from the menu. On the “Add Reference” dialog, pick the COM tab and scroll down to Microsoft Office 11.0 Object library. (It should be there if Outlook is correctly installed on your computer). Pick it and click OK.
Put the following declaration in your “using” section of Connect.cs:
using Outlook = Microsoft.Office.Interop.Outlook;
using System.Diagnostics;
using System.Reflection;
Just under your class declaration:
public class Connect: Object, Extensibility.IDTExtensibility2
add the following class member declarations:
Outlook.Application m_outlook;
Outlook.ExplorerClass m_explorerclass;
Outlook.Inspectors m_inspectors;
Then create a new method with the following signature:
private void InitializeAddIn().
Call InitializeAddIn from the OnConnection() function that the wizard created for you and assign m_Outlook as Outlook.Application to the application object that was passed in OnConnection().
So now you have an OnConnection function that looks like this (you can take out the applicationObject and addInInstance the wizard added):
public void OnConnection(object application, ...)
{
m_outlook = application as Outlook.Application;
InitializeAddIn();
}
Also add another class method with the following signature (no implementation for now):
public void Explorer_ExplorerEvents_10_Event_Close()
And another class method with the following signature (no implementation for now):
public void Inspectors_NewInspector(Outlook.Inspector inspector)
In InitializeAddIn() put the following code:
try
{
m_explorerclass =
m_outlook.ActiveExplorer() as Outlook.ExplorerClass;
if (m_explorerclass != null)
{
m_explorerclass.ExplorerEvents_10_Event_Close += new
Outlook.ExplorerEvents_10_CloseEventHandler
(Explorer_ExplorerEvents_10_Event_Close);
m_inspectors = m_outlook.Inspectors;
m_inspectors.NewInspector += new
Outlook.InspectorsEvents_NewInspectorEventHandler
(Inspectors_NewInspector);
}
catch (Exception ex)
{
MessageBox.Show("Error in Connect.InitializeAddIn: " + ex.Message);
}
Now let’s build our Add-in and make sure everything is OK. Builds? Good. Outlook still recognizes it? Better!
So what’s going on here? C# is quite verbose in its syntax, so it helps if you like to read when you look at the method and event names! A bit hard to get used to if you come from a C/C++ background - but I digress…
What we are doing here is getting a reference to the “ActiveExplorer” (which at this time is the main Outlook window). We are also “registering” as a listener to the Close Event of the Active Explorer (main Outlook window). This means that Outlook will notify us in the function
Explorer_ExplorerEvents_10_Event_Close
when it closes (more about why later).
Also we are telling Outlook to notify us when a new “Inspector” window opens in Inspectors_NewInspector.
An “Inspector” window is what Outlook calls the windows it brings up for the user to “inspect” things. So when the user opens a contact, for example, that is an “inspector” window. It is in that notification function that we will add our tab to the inspector window.
Here’s where the fun starts. To get a new tab/page on an Outlook “inspector” window we have to use interop.
Put this code in the Inspectors_NewInspector body:
String strMyTab = "My New Outlook Tab"; ((Microsoft.Office.Interop.Outlook.Pages)(inspector.ModifiedFormPages)) .Add(strMyTab); inspector.ShowFormPage(strMyTab);
Compile MyAddin and then open Outlook. Create a new contact, save it and close it. Now reopen the contact you just created. Voila! Your new tab should be there (for some reason I don’t understand the tabs don’t always show up on existing or new contacts before they are saved).
You now have a basic Outlook Add-in that creates and adds a tab to Outlook inspector windows as they open up. Congratulations! The next question might be “What do I do with the tab?” A good question. You can design the tab page as a form to your own liking, using the built in fm20.dll common controls. You can also host Active-X controls (your own or those of a third party) on the tab which would allow for a great deal of customization and integration.
Problems remain with our implementation, however. None of them are of our doing, but alas, we must deal with them.