Email – How to Create a Link to a Specific Email Message in Outlook

emacsemailmicrosoft-outlookorg-modeurl

I use Outlook as my email client at work, but I don't want to use it to manage my tasks and todos. (Instead I use plain text files and Emacs org-mode.) Since many todo items start out as mails in my inbox, I often need to reference these mails.

Is there some clever way to create a link (a URL) that opens a specific email in Outlook when clicked?

Best Answer

You can do this with a little bit of code in Outlook and a little bit of code in Emacs.

First, if you're using Outlook 2007 you'll need to enable Outlook URLs with a registry addition. Instructions and the registry file can be found here courtesy of David Tan.

Next, this macro can be added to Outlook and will get the GUID of the current email message, create a Org-Mode link and deposit it into the clipboard.

'Adds a link to the currently selected message to the clipboard
Sub AddLinkToMessageInClipboard()

   Dim objMail As Outlook.MailItem
   Dim doClipboard As New DataObject

   'One and ONLY one message muse be selected
   If Application.ActiveExplorer.Selection.Count <> 1 Then
       MsgBox ("Select one and ONLY one message.")
       Exit Sub
   End If

   Set objMail = Application.ActiveExplorer.Selection.Item(1)
   doClipboard.SetText "[[outlook:" + objMail.EntryID + "][MESSAGE: " + objMail.Subject + " (" + objMail.SenderName + ")]]"
   doClipboard.PutInClipboard

End Sub

As koushik noted in the comments, the doClipboard.SetText part can be expanded to differentiate between different item types:

If objMail.Class = olMail Then
    doClipboard.SetText "[[outlook:" + objMail.EntryID + "][MESSAGE: " + objMail.Subject + " (" + objMail.SenderName + ")]]"
ElseIf objMail.Class = olAppointment Then
    doClipboard.SetText "[[outlook:" + objMail.EntryID + "][MEETING: " + objMail.Subject + " (" + objMail.Organizer + ")]]"
ElseIf objMail.Class = olTask Then
    doClipboard.SetText "[[outlook:" + objMail.EntryID + "][TASK: " + objMail.Subject + " (" + objMail.Owner + ")]]"
ElseIf objMail.Class = olContact Then
    doClipboard.SetText "[[outlook:" + objMail.EntryID + "][CONTACT: " + objMail.Subject + " (" + objMail.FullName + ")]]"
ElseIf objMail.Class = olJournal Then
    doClipboard.SetText "[[outlook:" + objMail.EntryID + "][JOURNAL: " + objMail.Subject + " (" + objMail.Type + ")]]"
ElseIf objMail.Class = olNote Then
    doClipboard.SetText "[[outlook:" + objMail.EntryID + "][NOTE: " + objMail.Subject + " (" + " " + ")]]"
Else
    doClipboard.SetText "[[outlook:" + objMail.EntryID + "][ITEM: " + objMail.Subject + " (" + objMail.MessageClass + ")]]"    
End If

Almost there, add this little bit of lisp to your emacs lisp directory to enable Outlook links.

;;; org-outlook.el - Support for links to Outlook items in Org

(require 'org)

(org-add-link-type "outlook" 'org-outlook-open)

(defun org-outlook-open (id)
   "Open the Outlook item identified by ID.  ID should be an Outlook GUID."
   (w32-shell-execute "open" (concat "outlook:" id)))

(provide 'org-outlook)

;;; org-outlook.el ends here

And lastly, update your .emacs file to include the Outlook link code. Just add this somewhere after org-mode is setup.

(require 'org-outlook)

Now you can call the macro (I added it to my toolbar in Outlook for quick access) and you can quickly create a link to the email in Emacs.

One gotcha, GUID's change when you move a message between document stores, so if you get the GUID to the message while it's on your Exchange server and then move it to your local PST file the link will change. Move the message before you get the GUID.

Related Question