Friday, May 21, 2010

Random Task in Outlook

So maybe you have 20 minutes available and you want to do something, but you're not sure what. Or you have a tendency to delay the tasks you don't like to do. I find that leaving some decisions to luck is sometimes the best solution.

This VBA code will make Outlook select a random task from you to-do list, and then based on your feedback give you positive or negative reinforcements. In this case, if you do the task it shows a simple message and marks the task as complete, while not doing it displays a random post from Fail Blog. It was tested with Outlook 2010, but should work with any Outlook version that supports RSS feeds.

Please note that I will not be held responsible for any loss of productivity as a result of navigating Fail Blog!




Code:

Sub PickRandomTask()
    
    'Generic stuff necessary for Outlook Macros.
    Set objOutlook = CreateObject("Outlook.Application")
    Set objNamespace = objOutlook.GetNamespace("MAPI")
    
    'Selects incomplete tasks from your default tasks folder
    Set objFolder = objNamespace.GetDefaultFolder(olFolderTasks)
    Set colItems = objFolder.Items
    strFilter = "[Complete] = FALSE"
    Set coltasks = colItems.Restrict(strFilter)
    
    'Selects posts from an RSS Feed to display when a task is not completed.
    'Note: You have to previously add the RSS Feed to your Outlook.
    Set objFolder2 = objNamespace.Folders.Item("Personal Folders")
    Set objFolder1 = objFolder2.Folders.Item("RSS Feeds")
    Set objFail = objFolder1.Folders.Item("FAIL Blog: Epic Fail Funny Pictures and Funny Videos of Owned, Pwned and Fail Moments")
    Set FailPosts = objFail.Items
    
    'variables
    Dim n As Integer
    Dim f As Integer
    
    
    'Picks a random task for the user, and asks for feedback on task completion
    n = Int((coltasks.count) * Rnd + 1)
    Answer = MsgBox("Do this task: " & coltasks(n).Subject & vbCrLf & vbCrLf & "RIGHT NOW!", vbQuestion + vbYesNo, "???")
    
    If Answer = vbNo Then
        'Code for No
        f = Int((FailPosts.count) * Rnd + 1)
        MsgBox "EPIC FAIL!"
        FailPosts(f).Display
        
    Else
        'Code for Yes
        MsgBox "You are my hero!"
        coltasks(n).MarkComplete
    End If
    
End Sub

 This feature was inspired by  To -> done (a task management website based on randomizing the next task).

EDIT: Lifehacker (my personal Bible) linked to this post, added some instructions and removed the RSS part of the cripts (yeah, I know, it won't be very productive...). Check it out here.

I'll dedicate a few posts to VBA basics in the following days. If you're confused about how to use this stay tuned!

4 comments:

  1. A great idea! Thanks for your post! Unfortunately I am getting the following error:

    "Run-time error '-2147352567 (80020009)': Array index out of bounds."

    Do you know if there is a work-around for this? Thanks a lot for your help!

    ReplyDelete
  2. @Vincent - I had the same thing...because I had no uncompleted Tasks. Create one and see what happens.

    ReplyDelete
  3. @Toby - Thanks a lot! I just created a filter that automatically creates tasks out of emails as they come in. (I followed the example of http://goo.gl/cUyn). You can automatically turn all your emails into Tasks, or restrict yourself to colleagues or just your own to do's. It's still a slightly crude system, but the two work nicely together so far!

    ReplyDelete
  4. Well, I never run out of tasks so I never thought about that :)

    This code after the variables declaration will stop the error:

    If coltasks.count = 0 Then
    MsgBox "You have no uncomplete tasks. Are you sure you're not hiding something?"

    Else
    'Picks a random task for the user, and asks for feedback on task completion
    n = Int((coltasks.count) * Rnd + 1)
    Answer = MsgBox("Do this task: " & coltasks(n).Subject & vbCrLf & vbCrLf & "RIGHT NOW!", vbQuestion + vbYesNo, "???")

    If Answer = vbNo Then
    'Code for No
    f = Int((FailPosts.count) * Rnd + 1)
    MsgBox "EPIC FAIL!"
    FailPosts(f).Display

    Else
    'Code for Yes
    MsgBox "You are my hero!"
    coltasks(n).MarkComplete
    End If
    End If

    ReplyDelete