This article is about an Outlook AddOn (VSTO) to store the attachments in the file system. This AddOn will be shown as a Pane integrated in Outlook, so the user is able to simply drag-drop the Email into the appropriate folder. The folder will be automatically selected based on the current Email address.
Introduction
The goal of this project is to show how simple it is to create a custom pane in the Microsoft Outlook [3] and do some action based on the Email address of the current Email object:
This tool reads an address of current selected Email and shows in the pane on the right side the appropriate folders with its sub-folders.
Now, a user can easy drag-drop the Email in one of the sub-folders in order to save its attachment there.
The mapping between the Email addresses and the folders takes place in the configuration file, please take a look at OutlookAddOn.xml:
<file_system>
<folder>impressum@amazon.de|C:\TEMP</folder>
<folder>rechnungsstelle@1und1.de|D:\_Download</folder>
</file_system>
As you see, the address is separated from folder by pipe character like this:
<folder>Attachment_of_Emails_with_this_address ... | ... should_be_saved_in_sub-folders_of_this_folder</folder>
As the <folders>
is a list of entries, user is able to add as many addresses as he needs.
Background
The technique behind is the VSTO (Visual Studio Tools For Office). This API makes it possible to get all information of an Email object, for example, the count of attachments as well as the attachment itself.
Some registry entries are necessary when you deploy VSTO Add-ins that are created by using Visual Studio. These registry entries provide information that enable the Microsoft Office application to discover and load the VSTO Add-in [1].
When a VSTO Add-in is installed, it can be registered in two ways:
- For the current user only (that is, it is available only to the user that is logged on to the computer when the VSTO Add-in is installed). In this case, the registry entries are created under the
HKEY_CURRENT_USER
. - For all users (that is, any user that logs on to the computer can use the VSTO Add-in). In this case, the registry entries are created under
HKEY_LOCAL_MACHINE
.
This is an example of how to install Add-in for the current user:
Windows Registry Editor Version 5.00
[HKEY_CURRENT_USER\Software\Microsoft\Office\Outlook\Addins\OutlookAddOn]
"Description"="OutlookAddOn"
"FriendlyName"="OutlookAddOn"
"LoadBehavior"=dword:00000003
"Manifest"="H:/_Projekte/OutlookAddOn/bin/Release/OutlookAddOn.vsto|vstolocal"
[HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Vsto Runtime Setup\v4]
"EnableVSTOLocalUNC"=dword:00000001
You will need to replace the H:/_Projekte/OutlookAddOn/bin/Release/ with the path of the folder contained AddOn and to save this code snipped in a file *.reg. By executing this file (double click), all keys will be added to the windows registry:
How Outlook VSTO AddOn Works (Callbacks)
There are two important callback functions that are necessarily needed to get an AddOn working [4]:
The Startup
method - a kind of constructor, a perfect place to create our custom form / pane:
private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
try
{
oUserCtrl = new axUserCtrl(this);
...
And the Explorer.SelectionChange
event which will be fired every time an Email
item get selected:
private void CurrentExplorer_Event()
{
if (this.Application.ActiveExplorer().Selection.Count == 1)
{
Object selObject = Application.ActiveExplorer().Selection[1];
if (selObject is MailItem)
{
MailItem mailItem = (selObject as MailItem);
oUserCtrl.SetEmail(mailItem);
...
That is actually all that we need:
- We have created a custom form that will be shown as a pane embedded in Outlook, and
- We get a notification / callback every time the Email selection changed - the current Email item will be provided to the custom form!
One information that we need is the Email address to show an appropriated folder, the second information is the path to the attachment to store it into this folder.
Another important point is to use the worker thread wherever we do something in the background:
_workerThread = new Thread(_oWorker.GetResultList);
_workerThread.Start();
We should avoid the freezing of the GUI, otherwise our AddIn can be refused by Outlook.
Using the Code
The axFileSystemWorker
is derived from axBaseWorker
in order to show one of many possibilities, what can be achieved by using the VSTO interface:
enMode nMode = _oWorker.GetMode();
if (nMode == enMode.eFILE_SYSTEM)
_oWorker = new axFileSystemWorker(this) as iWorker;
We can easily implement a REST interface (POST
, GET
) or something else by deriving from axBaseWorker
and overriding the virtual methods, for example:
public virtual bool Connect()
{
throw new NotImplementedException();
}
Points of Interest
History