Build Outlook add-in in .NET to manage Outlook templates & signatures
I love small plug-ins. They are simple, quick, and reasonably priced. There is a small add-in, which I love with all my heart. It is Template Phrases for Outlook.
I want to walk you through this Outlook plug-in, show you how it works and also how to create such add-ins.
You can easily imagine how often the Add-in Express support team type the same words or phrases in their e-mails hundreds of time, explain the same things again and again to our customers and prospects: greetings, signatures, the same answers to the same questions about ordering, downloading, installing, activating, and using our products, etc.
Probably you need to do the same in your daily work. Of course, you can use signatures in Outlook, but it is not convenient if you have more than 10-15 of them.
The idea was born when we realized that we spent too much time on inserting standard phrases into our e-mails. Aren’t we Office developers? Didn’t we develop Add-in Express for building Office add-ins in .NET, Outlook, Excel and Word plug-ins in VSTO, and Office COM add-ins in Delphi. Didn’t we develop the Add-in Express Extensions for Outlook that allows embedding custom forms into Outlook windows? When we detected the problem and found the way to solve it, the life speeded up, you know how this happens.
Here is the result of our work.
The form embedded to the right side of the e-mail window above is a warehouse of phrases and blocks of text I use in my daily work. The screenshot above shows how you can organize your phrases in a folder tree. If you double-click the caption of the phrase template, the add-in pastes the phrase to where the text cursor stands in your e-mail. The add-in UI allows you to resize the form using the splitter. You can also use the button on the splitter to collapse / expand the form.
Now let’s create a sample COM add-in project to show a form like the one above.
You create a new Outlook COM add-in by choosing the Add-in Express COM Add-in project template in the New Project dialog in Visual Studio 2003 – 2008 or in the File | New | Other dialog in Delphi. VSTO developers create a COM add-in project and add an Add-in Express COM Add-in Module to the project via the Add New Item dialog.
The plug-in will display a form embedded into Outlook windows. To create such a form, you follow two steps below:
1. Add a new Outlook Form to your project and populate it with controls
2. Add Outlook Forms Manager to the add-in module, create an item in the Forms collection of the Forms Manager, choose the form the item controls, and specify context and UI properties for the form.
To create the form itself, you choose the Add-in Express Outlook Form item in the appropriate dialog of your development platform:
- For Add-in Express .NET projects, in the Add New Item dialog, you choose the Common Items / Add-in Express Items / Outlook / Add-in Express Outlook Form item
- For Add-in Express VSTO projects, in the Add New Item dialog, you choose the Common Items / Add-in Express for VSTO Items / Outlook / Add-in Express Outlook Form item
- In Add-in Express VCL projects, you choose the Add-in Express Outlook Form item in the File |New | Other dialog.
The form is a special descendant of System.Windows.Forms.Form (in Delphi, TForm). It provides a number of properties. In our Outlook plug-in, we will use the InspectorObj property. It returns the Outlook.Inspector COM object for the e-mail window that the form is shown in. Using this object, you can implement the most important functionality provided by the plug-in: pasting a string at the cursor location in the current message body.
To understand the scale of the problem, it’s enough to admit that Outlook shows the following e-mail inspector window types:
- Word
- HTML
- Plain text
- RTF
However, for the add-in that inserts text strings into the message, the last one, RTF, doesn’t make sense because Outlook uses this format to read (not edit) e-mail messages.
To recognize the message format, you can use the Inspector.EditorType property. Then, you work with the message text using the following routes:
EditorType | How to get the editor? | Comments |
Word | See the Inspector.WordEditor property. | Use the Word object model. |
HTML | See the Inspector.HTMLEditor property. | Use any HTML parser, say mshtml. |
Plain-text | Use Windows API to get the editor. | Use Text Object Model. |
Note also that when you use Outlook Templates plug-in to paste a phrase, the add-in returns focus to the editor window and enables the Edit | Undo menu item without faults. “I love this game”.
A notable part of the form is the toolbar at its top. To create such a toolbar, you can use built-in and third party controls. For more ideas, have a look at how to paint toolbar controls in the Office style.
Other tasty features of the plug-in are the result of usual developer’s work:
Feature | How to implement it? |
You can organize your templates in folders and sub-folders. | Any tree component will do it for you. |
The add-in allows importing / exporting template phrases using a file containing an XML markup. |
You can use any XML parser. |
You can assign a key combination to a phrase. | Add-in Express provides the keyboard shortcut component. A far longer way is creating a keyboard hook (see SetWindowsHookEx in MSDN). |
Macros in template phrases. | Well, it’s mostly a familiar find-and-replace. |
Enough with the form. Now we need to add an Outlook Forms Manager to the add-in module, add an item to its Items collection, and specify the item’s settings. We know that the form must be located on the right side of the mail inspector window. Here are the item’s properties to set:
Property | Value | Description |
InspectorItemTypes | Our form will be shown for mail inspector windows only. | |
InspectorLayout | Right | The form will be shown on the right side of the inspector window. |
FormClassName | This specifies the form we created on the previous step. |
That’s all for now. Hope you find it useful and enjoy using the Template Phrases for Outlook as I do.
Andrei Smolin
Add-in Express Team Leader