Working with All Items in a Folder or Selected Items

Last reviewed on January 26, 2015   —  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 item 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


About Diane Poremsky

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 Outlook forums by Slipstick.com.

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"

    1. Diane Poremsky

      My bad. :) When using currentfolder, it uses the application, when you call the folders by name, you need to use the namespace. I'll fix the notes I added earlier tonight without thinking about that. Sorry.

      You need
      Dim Ns As Outlook.NameSpace
      Set Ns = Application.GetNamespace("MAPI")
      instead of
      Dim objOL As Outlook.Application
      Set objOL = Outlook.Application

      or change this: Set objFolder = objOL.[snip]
      to Set objFolder = session.[snip]

    2. Ainsley

      Hi Diane,

      Sub GetValueUsingRegEx()

      Dim Ns As Outlook.NameSpace
      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 Ns = Application.GetNamespace("MAPI")
      Set objFolder = Ns.GetDefaultFolder(olFolderInbox).Folders("VBA")
      'Error in above line: The attempted operation failed. An object could 'not be found.
      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 Ns = Nothing

      End Sub

      Unfortunately, I tried using both of the corrections you mentioned but still getting error on the same line. Any idea why this is happening? :(

    3. Diane Poremsky

      it works fine here. Is there a folder named VBA under the Inbox? You'll get that error if the folder isn't found.

    4. Ainsley

      Hi Diane,

      After revising the code to the correction you mentioned, I still had an error on the same line code saying "An object could not be found". Any idea why? :(

    5. Diane Poremsky

      Without checking it first, I'll guess another oops on my part. :)

  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.

    1. Diane Poremsky

      Are they using two different data files? This line:
      Set objFolder = Ns.GetDefaultFolder(olFolderInbox).Folders("VBA2")
      is using the default data file's Inbox\VBA folder. If the folder you want to use is in the second account's data file, you need to either use current folder and select it before running the macro or use the getfolderpath function at http://www.slipstick.com/developer/working-vba-nondefault-outlook-folders/.

    2. Ainsley

      Hi Diane,

      Oh, got it. Thank you so much Diane!

  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.

    1. Diane Poremsky

      Steal the restrict code from http://www.slipstick.com/outlook/combine-outlook-calendars-print-one/ :
      'create the filter - this copies appointments today to 3 days from now
      sFilter = "[Start] >= '" & Date & "'" & " And [Start] < '" & Date + 3 & "'"

      ' Apply the filter
      Set ResItems = calItems.Restrict(sFilter)

      iNumRestricted = 0

      'Loop through the items in the collection.
      For Each itm In ResItems
      iNumRestricted = iNumRestricted + 1

Leave a Reply

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

This site uses XenWord.