First, a little context on why I wanted to do this in the first place. If you just want to get the source code then just scroll down to the "The source code".

A fair number of the things that make it on to my ToDo list are messages that I receive in Outlook. I've been using Outlook's flags to mark these items for follow up so that I could keep track of them. I use a different colored flag that denotes its own meaning as defined by me. This way I can flag an item using a action type (ToDo, Deferred, Waiting For, etc...). This has worked fairly well for me but once I had a large number of items flagged in Outlook (did I mention that I am a procrastinator?) it didn't really help me keep track of the things I needed to keep track of in any meaningful way. All I could even see was the complete list of flagged items but I couldn't break it down further into projects or context.

To overcome this limitation I've recently started managing my ToDo list outside of Outlook in a program called MyLifeOrganized (aka MLO) . MLO allows you to drag/drop Outlook items into MLO's task list. When you do this it creates a new MLO task using the subject of the dropped Outlook item for the task name. However MLO does something else really smart when you drag/drop an Outlook item. It not only put the text of the Outlook item into the notes, it will also create a hyperlink that will open up the original Outlook item when clicked. It does this by using Outlook's URL syntax which looks something like this:

Outlook:<entry_id> where <entry_id> is an Outlook Entry ID

Windows naturally understands this form of URLs. If you click on one it will cause Outlook to open the item referenced. This has allowed me to take Outlook items and create MLO items simply by dragging them to the MLO task list. In most cases this feature in MLO does exactly what I want, take an Outlook message that I need to follow up on and place it into my ToDo list. However sometimes this isn't exactly what I want. Sometimes I just want to place a link to Outlook items in the notes of an existing MLO item. Unfortunately MLO doesn't support this but there is a way to do it if you willing to do a little macro work in Outlook.

Update 6/4/2007: If you are using Office 2007 then you will probably need to enable the Outlook URL protocol handler so that hyperlinks to mail messages work.To do this requires editing the registry. You simple need to create these registry keys (substituting your installation paths of course):

  • HKEY_CLASSES_ROOT
    • outlook
      (Default) = URL:Outlook Folders
      URL Protocol=""
      • DefaultIcon
        (Default) = "C:\PROGRA~1\MICROS~2\OFFICE12\OUTLLIB.DLL,-9403"
      • shell
        • open
          • command
            (Default) = "C:\PROGRA~1\MICROS~2\OFFICE12\OUTLOOK.EXE" /select "%1"

The source code

Below is the source for a two VB macros that can be added to Outlook. These macros will loop over all of the currently selected messages, getting the subject and Outlook EntryID for each message. With these two pieces of information they then build a string of text with the message's subject and its Outlook URL, each on their own line. It extends this text string for each selected message and places the resulting text string on the clipboard. The end result is one block of text that contains the message subject followed by the Outlook URL for each selected message. This text can then be pasted into any document that understands hyperlinks. This includes the notes of MLO items as well as all of the other Microsoft Office applications. This macro will work with multiple items selected in the main Outlook window as well as from the opened window of a single Outlook message. To use it, simple invoke the CopyItemIDs() macro. You can bind this macro to a menu or toolbar button for easier access within Outlook.

Note: I should mention that if you are using Microsoft Exchange server, the message Entry ID can change on you and break any existing Outlook URLs. This unfortunately always happens if you move a message to another folder so if you plan on using this, only invoke this macro after you have moved the message to a new folder.

Update: There is just one more thing you must do before you run this script. You need to include a reference to FM20.dll, which is the Forms 2.0 library. This will allow you to use the DataObject to manipulate the clipboard. Thanks to 'Some Guy' who pointed this omission out.

Sub CopyItemIDs()
    Dim myOLApp As Application
    Dim myNameSpace As NameSpace
    Dim currentMessage As MailItem
    Dim ClipBoard As String
    Dim DataO As DataObject
    
    ' Housekeeping: set up the macro environment
    Set myOLApp = CreateObject("Outlook.Application")
    Set myNameSpace = myOLApp.GetNamespace("MAPI")
    
    ' Figure out if the active window is a list of messages or one message
    ' in its own window
    On Error GoTo QuitIfError    ' But if there's a problem, skip it
    Select Case myOLApp.ActiveWindow.Class
        ' The active window is a list of messages (folder); this means there
        ' might be several selected messages
        Case olExplorer
            ' build the clipboard string
            For Each currentMessage In myOLApp.ActiveExplorer.Selection
                ClipBoard = GetMsgDetails(currentMessage, ClipBoard)
            Next
             
        ' The active window is a message window, meaning there will only
        ' be one selected message (the one in this window)
        Case olInspector
            ' build the clipboard string
            ClipBoard = GetMsgDetails(myOLApp.ActiveInspector.CurrentItem, _
                                         ClipBoard)
        ' can't handle any other kind of window; anything else will be ignored
    End Select
    
QuitIfError:       ' Come here if there was some kind of problem
    Set myOLApp = Nothing
    Set myNameSpace = Nothing
    Set currentMessage = Nothing

    Set DataO = New DataObject
    DataO.Clear
    DataO.SetText ClipBoard
    DataO.PutInClipboard
    
    Set DataO = Nothing

End Sub

Function GetMsgDetails(Item As MailItem, Details As String) As String

    If Details <> "" Then
        Details = Details + vbCrLf
    End If
    Details = Details + Item.Subject + vbCrLf
    Details = Details + "Outlook:" + Item.EntryID + vbCrLf

    GetMsgDetails = Details

End Function

Comments (19) -

Some Guy
Some Guy
10/25/2006 7:55:30 PM #

Under Outlook 2003 I get "User defined type not defined" on the line dimming the DataObject.  Can't figure it out.  Any ideas?

Some Guy
Some Guy
10/25/2006 8:06:31 PM #

Oh, hey, I figured out the "not defined" problem.  You have to go to References and browse to FM20.DLL which is the Forms 2.0 Library or the DataObject won't work.

David Jade
David Jade
10/25/2006 9:27:53 PM #

Sorry about leaving that bit out. Yes you have to include the reference. Glad you figured it out though. I will update the article ASAP.

david

Mark
Mark
11/17/2006 10:44:52 PM #

Could you explain in more detail how to reference FM20.DLL? I assume it is in the Outlook Visual Basic editor window, but as a novice I am not sure where. Thanks.

Mark

David Jade
David Jade
11/19/2006 2:52:29 PM #

To add the FM20.dll as a reference to your VBA macro project, open the Tools | References menu option in the VBA editor. From there you can place a check on the "Microsoft Forms 2.0 Object Library" item. If the that item in not in the list you will have to use the browse button first to find and add the fm20.dll file which should be located in you windows system directory.

John Crenshaw
John Crenshaw
2/20/2007 5:07:36 PM #

Works great in OL 2003 and earlier, (you can even use the name instead of the ID and it works for folders too), but it fails when I try the same thing with outlook 2007. Did MS remove the entire Outlook: systax?

Jignesh thar
Jignesh thar
3/20/2007 5:33:02 AM #

Works like a charm. Thanks!!

samuel renteria
samuel renteria
3/30/2007 10:29:41 PM #

Did MS remove the entire Outlook: systax?

David Jade
David Jade
3/30/2007 10:40:38 PM #

I don't think so but I have not been able to try it yet. I have seen reports though of people having issues with it so something may be currently broken.

I can't imagine that they have removed it especially given that they have just added something similar to the new version of OneNote.

david

tony
tony
8/15/2007 9:35:42 AM #


Outlook 2007 registry change ( see above ) might now be

(Default) = "C:\PROGRA~1\MICROS~2\OFFICE12\1033\OUTLLIBR.DLL,-9403"

Mike Petonic
Mike Petonic
8/20/2007 9:00:47 PM #

Thanks for munging this all together for us.  Excellent!  I use MLO as well, and am a VBA rookie.

What's really stiffling me is that there doesn't seem to be a programmatic way of not changing the OID of message.  I've made a post on the MLO forum...  Perhaps you could take a look there and see if this problem is solveable?  

groups.google.com/.../b3f9d3bdc742ab49



I'd love to have a macro that just moved the current folder into the deleted folders, created a text OID and put it in the clipboard.  

-Mike

David Jade
David Jade
8/20/2007 9:42:07 PM #

Yeah, there is no way to not have the OLID change on you. It is by-design for Outlook unfortunately.

david

Des
Des
10/2/2007 8:53:20 AM #

Absolutely brilliant...thanks!

Not sure if any VBA wizards can point me in right direction to top / tail this as follows.......?

After determining that a message in 'Inbox' is actionable I would love to be able to run this macro and have it: a) move the currently selected message to 'Action' folder; b) copy OL entry ID to clipboard; c) paste hyperlink in body of task (preferably preceded by date of message); and finally d) move cursor to task subject field ready for user input.

Any help / guidance greatly appreciated.

Thanks
Des

Erik
Erik
10/13/2010 10:35:45 PM #

Would the registry modification also work with Office 2010?

I haven't tried it on a computer with Office 2007 or 2010 yet, but I can not find Outllib.dll on either system.

David Jade
David Jade
10/13/2010 10:40:22 PM #

The key that use Outllib.dll is only used for the Exporer icon. I have not tried this with 2010 yet, but with 2007 it works just fine if you omit the DefaultIcon parts.

Hao Pang
Hao Pang
12/29/2010 8:16:49 PM #

I can't make the link work correct in Access if I save it in a Hyperlink field.

The reason is, the link is not in the format Access like. So I change the function GetMsgDetails and add a "#" in front of "outlook:" tag and now it works, like "#outlook:".

No idea why need to do this.

And thank you for your code, works magically.

Malte Neubecker
Malte Neubecker
4/15/2011 3:18:14 PM #

I use outlook2007. This macro works fine for mails, but for other items out of the calender, contacts, or tasks it never enters the loop "For Each currentMessage In myOLApp.ActiveExplorer.Selection". Any idea why? Or how to fix it?

Thanks
Malte

Roman Shein
Roman Shein
6/7/2011 5:15:33 AM #

Why not just drag'n'drop (by left click) an email to the task folder? It will immidiately create the task with the text of the message within the notes.

PJ IN FL
PJ IN FL
8/27/2011 5:14:14 PM #

Is there any way to write the VBA code without using the reference to the FM20.DLL library? Cannot set a reference in a "locked down" user account.

Comments are closed

Flux and Mutability

The mutable notebook of David Jade