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:
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
If (item.Class = olMail) And (item.UnRead) Then
' This message has not been read. Display it modally
Set msg = item
' uncomment the next line to have it only find one unread
' message at a time
' 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"
MsgBox Err.Description, vbCritical, Err.Number
To make this work,
- hit alt-F11 to show the macro window,
- double-click ThisOutlookSession
- and paste this code into the resulting window.
- Save it, then back in the Outlook primary window, right-click the button bar and choose Customize....
- On the "Commands" tab, scroll down to "Macros" in the Categories window and click "Macros"
- 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.