Outlook VBA: work with open item or selected item

Last reviewed on October 8, 2012

To work with the currently selected item, you'll need to use Explorer code to get the item in the Explorer:

Set objItem = objApp.ActiveExplorer.Selection.Item(1)

When you are looking at an item that is open and has focus, it uses an Outlook Inspector. The code you need to work with Inspectors is:

Set objItem = objApp.ActiveInspector.CurrentItem

When you want to use the macro with both open or selected items, you can use this function to determine if the item is open or selected. You'll call the function in your code in this manner:

Set objItem = GetCurrentItem()

You'll need to put one copy of this function in a module and can use the function in many macros.

Function GetCurrentItem() As Object
    Dim objApp As Outlook.Application
           
    Set objApp = Application
    On Error Resume Next
    Select Case TypeName(objApp.ActiveWindow)
        Case "Explorer"
            Set GetCurrentItem = objApp.ActiveExplorer.Selection.Item(1)
        Case "Inspector"
            Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
    End Select
       
    Set objApp = Nothing
End Function

Written by

Diane Poremsky
A Microsoft Outlook Most Valuable Professional (MVP) since 1999 and involved in IT support since 1985, Diane is the author of several books and video training CDs and online training classes for Microsoft Outlook. You can find her helping people online in Outlook Forums as well as in the Microsoft Answers and TechNet forums.