Working with All Items in a Folder or Selected Items

Last reviewed on August 7, 2014

These two samples contains the basic code for working with all items in a selected folder or selected items in a folder. The code sample prints the item subject in the Immediate window (Ctrl+6).

Because the code uses generic objects instead of specific objects, it will work with all items types.

Work with all items in any folder

Option Explicit
Public Sub DoSomethingFolder()
    Dim objOL As Outlook.Application
    Dim objOutlookItem As Object
    Dim objItems As Outlook.Items
    Dim objFolder As Outlook.MAPIFolder
    Dim obj As Object
 
 
    Set objOL = Outlook.Application
    Set objFolder = objOL.ActiveExplorer.CurrentFolder
    Set objItems = objFolder.Items
 
    For Each obj In objItems
 
     With obj
 
    ' do whatever
       Debug.Print .Subject
     
     End With

    Next
 
    Set obj = Nothing
    Set objOutlookItem = Nothing
    Set objItems = Nothing
    Set objFolder = Nothing
    Set objOL = Nothing
End Sub


Work with Selected items in any folder

Option Explicit

Public Sub DoSomethingSelection()
    Dim Session As Outlook.NameSpace
    Dim currentExplorer As Explorer
    Dim Selection As Selection
    
    Dim obj As Object

    Set currentExplorer = Application.ActiveExplorer
    Set Selection = currentExplorer.Selection


    For Each obj In Selection
 
     With obj
 
    ' do whatever
       Debug.Print .Subject
     
     End With

    Next

    Set Session = Nothing
    Set currentExplorer = Nothing
    Set obj = Nothing
    Set Selection = Nothing


End Sub


Written by

Diane Poremsky
A Microsoft Outlook Most Valuable Professional (MVP) since 1999, Diane is the author of several books, including Outlook 2013 Absolute Beginners Book. She also created 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.

Please post long or more complicated questions at Outlookforums.

Leave a Reply

If the Post Coment button disappears, press your Tab key.