Tuesday, August 19, 2008

Outlook: VBA Script to display all unread messages

I wanted a button on Outlook that would let me catch up with all my emails by opening each unread email one by one. There is a button on the reader pane for this, but I specifically wanted to be able to do this quickly from the main window. Also I was casting about for something geeky to do and this caught my eye.

Here is the code to make it happen:


Sub find_unread()
On Error GoTo eh:
' I want to be able to catch up by reading all my unread messages
Dim ns As Outlook.NameSpace
Dim folder As MAPIFolder
Dim item As Object
Dim msg As MailItem

' Open the inbox folder
Set ns = Session.Application.GetNamespace("MAPI")
Set folder = ns.GetDefaultFolder(olFolderInbox)

' Loop through items in the inbox folder
For Each item In folder.Items
DoEvents
If (item.Class = olMail) And (item.UnRead) Then
' This message has not been read. Display it modally
Set msg = item
msg.Display True
' uncomment the next line to have it only find one unread
' message at a time
'Exit For
End If
Next

' If you uncommented the line to read individual messages,
' comment the next line so you don't get a message box
' every single message!

MsgBox "All messages in Inbox are read", vbInformation, "All Read"
Exit Sub
eh:
MsgBox Err.Description, vbCritical, Err.Number
End Sub

To make this work,
  1. hit alt-F11 to show the macro window,
  2. double-click ThisOutlookSession
  3. and paste this code into the resulting window.
  4. Save it, then back in the Outlook primary window, right-click the button bar and choose Customize....
  5. On the "Commands" tab, scroll down to "Macros" in the Categories window and click "Macros"
  6. Select your macro in the "Commands" window and drag it over to the button bar.

You can clean up the button if you like.

To use the command, press the button and each unread message will open, one at a time. When you close a message, the next message will open.

8 comments:

  1. Is there anyway to get all the windows to display at once (ie the display in cascading windows and you don't need to close one to read the others?)

    Thanks

    ReplyDelete
    Replies
    1. Hi Melvin,
      Change the line
      msg.display true
      to:
      msg.display false

      That will open all the unread messages. Hope it works for you!

      Delete
  2. Melvin, I'm sure there is. Let me have a look at the script tomorrow when I am back in the office and I will see what I can do.

    ReplyDelete
  3. Hey, I was just messing around and if you get rid of the True after msg.Display True
    Then it will open everything at once

    Thanks for the code

    Is it okay if I ask more questions?

    ReplyDelete
  4. Hello Timothy, I know this post is old but hoping you're still interested in maybe tweaking it a bit?? I'm currently using Outlook 2010, likely to move to Outlook 2013 pretty soon (not the other, cloud version) and recently converted all of my accounts to IMAP. The one thing I miss that I haven't been able to create/find is a way to view ALL of my unread emails from across multiple IMAP accounts; I was able to set up your VBA Script to view items in my default inbox (which coincidentally is a local PST used only for calendar/contacts and therefore doesn't contain anything) but can't figure out how to make it pull from multiple IMAP accounts all at once. I did find this site and tried to integrate it into your script above but couldn't make that happen. Any help would be greatly appreciated, but also understood if you have other priorities!!
    Use a folder in another pst or Mailbox: http://www.slipstick.com/outlook-developer/working-vba-nondefault-outlook-folders/

    ReplyDelete
  5. David, let me have a look and get back. It might be tomorrow before I can get to it.

    ReplyDelete
  6. Hi ,

    Need your help to write the Macro for the specified folder, which Automatically reminds every 45 minutes , the number of unread of messages in that particular folder

    ReplyDelete
  7. Hello Timothy,

    How could I make this code also work for subfolders I have on my Inbox.

    ReplyDelete

I moderate comments blog posts over 14 days old. This keeps a lot of spam away. I generally am all right about moderating. Thanks for understanding.