Karachi   ->   Sweden   ->   Karachi, again   ->   Dubai   ->   Bahrain   ->   Karachi, once more   ->   London and Leeds

Saturday, December 15, 2007

Programming MS Outlook

I always hate when I send somebody an email without a subject; and it happens too often with official emails when I am using MS Outlook.

There are various ways to program Outlook: Visual Studio Tools for Office (VSTO), COM Addins and Visual Basic for Applications (VBA). As far as VSTO is concerned, the idea of programming add-ins in C# or other .NET languages may seem fascinating but the requirements of a heavy runtime, as well as the idiosyncrasies of .NET framework versions (and frequent API changes) will cause you enough trouble to drop the idea.

Then we are left with COM Addins and Visual Basic for Applications. While the preferred way in terms of security and redistribution is COM Addins, VBA isn't bad either. Nevertheless, you need to understand Outlook object model, no matter what paradigm you adhere to. So, here is a very quick solution to trapping blank subject of outgoing emails using VBA. It isn't hard to convert the code to a compiled COM Addin.

Private Sub Application_ItemSend (ByVal Item As Object, 
Cancel As Boolean)
If (Not Item.Class = olMail) Then Exit Sub

If (Item.Subject = "") Then
answer = MsgBox("Do you want to send the message without entering a subject",
vbYesNo + vbSystemModal)
If (answer = vbNo) Then
Cancel = True
End If
End If
End Sub


Where to put in this code? Hit Alt-F11 in MS Outlook to open up the VBA editor, and copy n' paste this code. Make sure that you set the macro security to Medium so that Outlook asks you before enabling/ disabling a macro.


Programming Outlook 2007 by Ken Slovak from Wrox is a no nonsense book. If you have ever participated in the Outlook newsgroup, you already know who Ken Slovak is. You should get it if you want a head-start in Outlook programming. The only caveat is that the book discusses Outlook 2007, with very few lessons on programming older versions of the office application.