Working with All Items in a Folder or Selected Items

Last reviewed on November 19, 2014   —  11 comments

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


To work with all items in a specific folder, replace
Set objFolder = objOL.ActiveExplorer.CurrentFolder
with GetDefaultFolder and locate the folder (if it's not a default folder).
Set objFolder = Ns.GetDefaultFolder(olFolderCalendar)
Set objFolder = Ns.GetDefaultFolder(olFolderCalendar).Folders("Subfolder")

You'll also need to add these two lines to the macro - Dim Ns should be the first Dim statement, and Set Ns needs to be the first Set statement.

Dim Ns As Outlook.NameSpace
Set Ns = Application.GetNamespace("MAPI")

See VBA and non-default Outlook Folders for more information.

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.

11 responses to “Working with All Items in a Folder or Selected Items”

  1. Ainsley

    Hi Diane,

    I tried to use this as sample to run a code on all items in a subfolder named "VBA Outlook Test" under the Inbox but however turned out to be error. My code has this structure:

    Sub GetValueUsingRegEx()

    Dim objOL As Outlook.Application
    Dim objItems As Outlook.Items
    Dim objFolder As Outlook.MAPIFolder
    Dim olMail As Object
    Dim Reg1 As RegExp
    Dim M1 As MatchCollection
    Dim M As Match

    Set objOL = Outlook.Application
    Set objFolder = objOL.GetDefaultFolder(olFolderInbox).Folders("VBA Outlook Test") 'Error located on the above line
    Set objItems = objFolder.Items

    For Each olMail In objItems

    'All the working codes here

    Next
    Set olMail = Nothing
    Set objOutlookItem = Nothing
    Set objItems = Nothing
    Set objFolder = Nothing
    Set objOL = Nothing

    End Sub

    Could you please advise what is wrong? Error says "Object does not support this property or method"

  2. Ainsley

    Hi Diane,

    Yes there is a folder named "VBA" under my inbox. However, I had two email accounts under my outlook (one is a shared email account while another is a personal one). Will this affect the coding? I need the code to run only on one of the account.

  3. Russ B.

    Thank you Diane for answering my other questions I have posted in other sections... You are great and I have become a VBA demon. Now I am trying to take a specific day's appointments and then load them into an excel sheet that is published to our intranet daily. The above code seems to grab everything in the calendar folder. How would I restrict it to a specific day? For example if my calendar is currently viewing the appointments for December 5th, 2014 I would want to run a macro that would grab that information for each appt. on the currently viewed day. I think I can manage the rest once I have the appointment info for that day. Any help is always appreciated.

Leave a Reply

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