Justin Cooney

Web Development Tips and Examples

Background

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:

  1. Open your old project(s)
  2. Expand the Build top menu
  3. 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

References

Posted in , , , , , , , ,

3 responses to “Outlook Custom Add-In to Archive Emails on Send Based on Keyword in Subject Line (Visual Studio 2010)”

  1. […] a previous article I wrote about how to create a custom add-on for Outlook that will archive emails based on a keyword in the Email’s subject l…. I will expand on this concept to show how to convert the sent email from an […]

  2. […] Add-in components using Visual Studio 2010. In previous articles I have covered things such as adding custom actions to archive email messages via the Application_ItemSend event, as well as adding custom code to save Outlook email messages to a database in the .MSG […]

  3. […] Your code that backs up each email is responsible for adding the necessary UDFs to each message. If you want information on how to back up your email messages, see an article I wrote titled: Outlook Custom Add-In to Archive Emails on Send Based on Keyword in Subject Line (Visual Studio 201…. […]

Leave a comment