The great thing about the Microsoft technology stack is that things tend to work together without the need to download and install unfamiliar libraries from unknown sources. I continue to be impressed by how Microsoft centralizes its code libraries with Visual Studio and .NET 4 to make most development tasks quite seamless.
For example I am working on an add-on component for MS Outlook 2007 that will help people archive their sent emails by including a keyword in the subject line of their email.
Visual Studio has default project templates for Office, including templates for Outlook 2007 and Outlook 2010. Happily enough, the code required to interface with Outlook is auto-generated behind the scenes and the programmer simply has to worry about writing the actual logic of the component. This is a big step forward from how things used to be!
Another big plus for my development efforts is the set-up and deployment project template that Microsoft has included with Visual Studio. Once my Outlook component has been fully coded I can simply create a Deployment project to send out to users. That way a user can install my component simply by clicking the ‘Next’ button on an installer wizard.
Description of the Outlook Component
The desired behavior for the Outlook Add-in I am working on is that it run invisibly in the background when a user works with their MS Outlook email. The component code will only kick in when the user enters a specific string in the subject line of their email and clicks the Send button.
Once a keyword has been included and the Send button has been clicked, the add-in component should look for a related folder in the user’s Outlook inbox. If no folder exists then one should be created. Then, once this is done, the add-in component should copy the email being sent into the archiving folder.
Details of the Solution
I am including example code below for the Outlook add-in. This code toggles from the keyword ‘spec’ in the subject line of the email.
The logic for activating the component code when the Outlook email Send button is clicked can be done by accessing the Application.ItemSend event.
Within the Application.ItemSend event, the actual email message is referenced using Outlook.MailItem, from which the following functionality can be handled:
- to check the Subject line (using mailItem.Subject)
- to copy the email message can be accessed (using mailItem.Copy)
- to move the copy to an archive folder (using mailItemCopy.Move(folder))
As well, to check for a sub-folder in the user’s Inbox folder, one can use Outlook.MAPIFolder and get the default Inbox folder from this using Outlook.OlDefaultFolders.olFolderInbox.
Final Step: Cleaning up Your Project
An important step while developing for Outlook is to remove the add-in assembly, registry settings, and security settings from your computer. For example I developed two separate test projects which I did not remove from the computer, so when I opened up a third test project, the test conditions of the first two projects were still active.
Fortunately cleaning old project settings from your computer is simple. In Visual Studio:
- Open your old project(s)
- Expand the Build top menu
- Click the Clean Solution option.
Code for the VB.NET Outlook Add-In
Public Class ThisAddIn Const FOLDER_NAME As String = "spec" Private Sub ThisAddIn_Startup(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Startup End Sub Private Sub Application_ItemSend(Item As Object, ByRef Cancel As Boolean) Handles Application.ItemSend Dim mailItem As Outlook.MailItem = TryCast(Item, Outlook.MailItem) If (mailItem IsNot Nothing) Then If (mailItem.Subject).ToLower().Contains(FOLDER_NAME) Then Dim mailItemCopy As Outlook.MailItem = TryCast(mailItem.Copy, Outlook.MailItem) Dim inbox As Outlook.MAPIFolder = Me.Application.GetNamespace("MAPI").GetDefaultFolder(Outlook.OlDefaultFolders.olFolderInbox) Dim folder As Outlook.MAPIFolder = Nothing Try folder = inbox.Folders(FOLDER_NAME) Catch ex As System.Runtime.InteropServices.COMException Catch ex As Exception MsgBox(ex.Message) End Try If (folder Is Nothing) Then folder = inbox.Folders.Add(FOLDER_NAME, Outlook.OlDefaultFolders.olFolderInbox) End If mailItemCopy.Move(folder) End If End If End Sub End Class
- MSDN Office 2010 Application Events
- MSDN Visual Studio 2010 Outlook Solutions
- CodeProject.com – Outlook 2007 Add-in Using Microsoft Visual C#.NET
- MSDN Visual Studio 2010 – Walkthrough: Creating Your First Application-Level Add-in for Outlook
- MSDN Visual Studio 2010 – Office UI Customization
- MSDN Magazine – Improve Your Outlook With Visual Studio For Office
- MSDN Blogs – MCD UK Solution Development Team – Building and Deploying an Outlook 2010 Add-in (part 1 of 2)
- MSDN Blogs – MCD UK Solution Development Team – Building and Deploying an Outlook 2010 Add-in (part 2 of 2)
- Add-In-Express Blogs – HowTo: Handle the Outlook ItemSend Event
- Books.google.ca – Visual Studio Tools for Office
- MSDN Office 2007 – Developing Interfaces in Outlook 2007
- MSDN Office XP – Building Outlook 2002 Add-ins with Visual Basic.NET
- MSDN Visual Studio 2010 – Creating Outlook Form Regions
- MSDN Visual Studio 2010 – How to: Add a Custom Action to an Outlook Form Region
- MSDN Visual Studio 2010 – Deploying Office Solutions