• Outlook User
  • New Outlook app
  • Outlook.com
  • Outlook Mac
  • Outlook & iCloud
  • Developer
  • Microsoft 365 Admin
    • Common Problems
    • Microsoft 365
    • Outlook BCM
    • Utilities & Addins

Working with All Items in a Folder or Selected Items

Slipstick Systems

› Developer › Code Samples › Working with All Items in a Folder or Selected Items

Last reviewed on September 14, 2022     65 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 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 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 objOL As Outlook.Application
    Dim currentExplorer As Explorer
    Dim Selection As Selection
    
    Dim obj As Object
    Set objOL = Outlook.Application
    Set currentExplorer = objOL.ActiveExplorer
    Set Selection = currentExplorer.Selection

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

    Next

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

End Sub

Working with All Items in a Folder or Selected Items was last modified: September 14th, 2022 by Diane Poremsky

Related Posts:

  • Use VBA to get an Appointment's Time Zone
  • Save Messages as *.DOC or *.DOCX File Type
  • Macro to Resend Sent Message
  • Set a reminder on selected items in the To-Do List

About 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.

Subscribe
Notify of
65 Comments
newest
oldest most voted
Inline Feedbacks
View all comments

John (@guest_220241)
April 26, 2023 1:22 pm
#220241

Is there a way to get all of the email items in an Outlook folder regardless if they're on the Exchange server or just in Outlook? The policy where I work won't allow me to change the caching option. I was hoping for a property that I could set or method to pull them all in.

0
0
Reply
Vidal Ccori (@guest_219590)
August 3, 2022 9:05 am
#219590

Option Explicit
Public Sub DoSomethingFolder()
    Dim objOL As Outlook.Application
    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 objItems = Nothing
    Set objFolder = Nothing
    Set objOL = Nothing
End Sub

Please, how can I make it go through the mail starting from the most recent mail to the oldest?
In Outlook 14 you loop from newest to oldest and Outlook 16 loops from oldest to newest.

0
0
Reply
Sathiya (@guest_219549)
July 18, 2022 12:57 pm
#219549

Hi Diane, hope you're doing fine, how do I specify certain links to be opened in an email, for example, if I have an email that has 6 links but I only want to open 3 links out of it, how do I define the pattern so that it only opens those 3 instead of all, let's say http://www.google.com, http://www.gmail.com, and http://www.ymail.com.

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Sathiya
July 19, 2022 12:05 am
#219551

you need an if statement -

You use this if you want to skip the url -
If InStr(strURL, "google.com") Then GoTo NextURL

If you want to skip all but certain sites, this should work
If InStr(strURL,"google.com") = 0 Then GoTo NextURL

1
0
Reply
Sathiya (@guest_219554)
Reply to  Diane Poremsky
July 19, 2022 11:08 am
#219554

Hi Diane,

It works only on one, is there a way I can add multiple? for example, when I say
If InStr(strURL,"google.com") = 0 Then GoTo NextURL it opens Google.com however I still have Ymail & Gmail in the email which I want to open may I know how do I add them?

If InStr(strURL,"google.com") = 0 Then GoTo NextURL
If InStr(strURL,"ymail.com") = 0 Then GoTo NextURL
If InStr(strURL,"gmail.com") = 0 Then GoTo NextURL

I tried the above still it only opens "Google" but doesn't open "ymail & gmail"

I'm sorry if this is a dumb question, I'm a novice, your help is highly appreciated.

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Sathiya
July 20, 2022 1:37 am
#219561

Logic error. :) Goto NextURL skips the other two.

This should work -
If InStr(strURL,"google.com") = 0 Then
If InStr(strURL,"ymail.com") = 0 Then
If InStr(strURL,"gmail.com") = 0 Then GoTo NextURL
End if
End if

1
0
Reply
Sathiya (@guest_219563)
Reply to  Diane Poremsky
July 20, 2022 7:59 am
#219563

Thanks a Million! You're a life saver!

0
0
Reply
Sathiya (@guest_219503)
July 5, 2022 8:52 am
#219503

Hi Diane, When I used the code it only opens only the links on the first email even though the. Global value is set to True, there are 12 emails in the folder and it always opens the links on the first email, how do I get it to open all, your help is highly appreciated.

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Sathiya
July 9, 2022 12:23 am
#219526

global opens all links in the selected message. If you want to open links in the selected message, you need to use code that loops the selection.
https://www.slipstick.com/developer/code-samples/open-hyperlinks-email-message/#selected

1
0
Reply
Sathiya (@guest_219532)
Reply to  Diane Poremsky
July 12, 2022 11:26 am
#219532

Thanks a Million! one follow-up question, say I've ~10 hyperlinks in which I only want to open 3 specific ones, just for example say those are Ymail, Gmail & Google how do I modify the ".Pattern" code to let the script know that I only want to open the above 3, I tried different methods but no go, as always your help is highly appreciated!

0
0
Reply
Kashif (@guest_219096)
January 21, 2022 9:59 am
#219096

Hi Diane,

Thanks for the tutorial, I hope you are doing well, I have one question to you, can I move the mails (only those mails that have category blank) from a folder to another folder at once like copy and paste not through loop, move bulk mails at once, is this possible? if yes? please help me.

Like if we do manually then will go to that folder then apply filter the mails then select those filtered mails and drage those mails to another folder, at once we moved all the mails to difference folder.

Please take good care of yourself and your family.

Thanks
Kashif

0
0
Reply
Joseph (@guest_219068)
January 9, 2022 11:32 pm
#219068

Diane - you amaze me. I'm relatively new and I always end up on your site for solutions.

I want to display collection of MailItems I've captured in my current Explorer window. Basically, I want to do the equivalent of an instant search but programmatically...

I can't figure out how to get teh collection to display ... :/

set olExplorer = ActiveExplorer
olExplorer.Display(itmEmails)

That's clearly not right.

Can you push me in the right direction?

0
0
Reply
Nidhi (@guest_218848)
November 1, 2021 7:57 am
#218848

Hi! Can someone help me with a macro wherein I am trying to extract information from the emails, but not from all the emails. The macro needs to filter the emails based on the subject of the email and then extract information from the same?

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  Nidhi
November 2, 2021 10:42 am
#218850

This will get values from the message - as long as nothing in the other messages match, it could run on all messages in the folder.
Use RegEx to extract text from an Outlook email message (slipstick.com)

Otherwise you need to either use an if statement to check the subject -
If instr(1, item.subject, "phrase" > 0 then
' extract
end if

or use a find or restrict filter then run the macro on the filtered versions - this would be faster if there were "a ton" of messages and only a few needed to be processed.

0
0
Reply
DNDanello (@guest_218689)
August 31, 2021 9:19 am
#218689

So if I wanted to select only the items visible in a filtered view of the folder, would it be:

  Set currentExplorer = objOL.ActiveExplorer
  Set Selection = currentExplorer.SelectAllItems

?

Thx, you're awesome.

0
0
Reply
Diane Poremsky(@diane-poremsky)
Author
Reply to  DNDanello
November 2, 2021 10:43 am
#218851

It would still be the same as with a selection -

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

  Next

1
0
Reply

Visit Slipstick Forums.
What's New at Slipstick.com

Latest EMO: Vol. 30 Issue 19

Subscribe to Exchange Messaging Outlook






Support Services

Do you need help setting up Outlook, moving your email to a new computer, migrating or configuring Office 365, or just need some one-on-one assistance?

Our Sponsors

CompanionLink
ReliefJet
  • Popular
  • Latest
  • Week Month All
  • Jetpack plugin with Stats module needs to be enabled.
  • Open Outlook Templates using PowerShell
  • Count and List Folders in Classic Outlook
  • Google Workspace and Outlook with POP Mail
  • Import EML Files into New Outlook
  • Opening PST files in New Outlook
  • New Outlook: Show To, CC, BCC in Replies
  • Insert Word Document into Email using VBA
  • Delete Empty Folders using PowerShell
  • Warn Before Deleting a Contact
  • Classic Outlook is NOT Going Away in 2026
Ajax spinner

Recent Bugs List

Microsoft keeps a running list of issues affecting recently released updates at Fixes or workarounds for recent issues in classic Outlook (Windows).

For new Outlook for Windows: Fixes or workarounds for recent issues in new Outlook for Windows .

Outlook for Mac Recent issues: Fixes or workarounds for recent issues in Outlook for Mac

Outlook.com Recent issues: Fixes or workarounds for recent issues on Outlook.com

Office Update History

Update history for supported Office versions is at Update history for Office

Outlook Suggestions and Feedback

Outlook Feedback covers Outlook as an email client, including Outlook Android, iOS, Mac, and Windows clients, as well as the browser extension (PWA) and Outlook on the web.

Outlook (new) Feedback. Use this for feedback and suggestions for Outlook (new).

Use Outlook.com Feedback for suggestions or feedback about Outlook.com accounts.

Other Microsoft 365 applications and services




New Outlook Articles

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Google Workspace and Outlook with POP Mail

Import EML Files into New Outlook

Opening PST files in New Outlook

New Outlook: Show To, CC, BCC in Replies

Insert Word Document into Email using VBA

Delete Empty Folders using PowerShell

Warn Before Deleting a Contact

Classic Outlook is NOT Going Away in 2026

Newest Code Samples

Open Outlook Templates using PowerShell

Count and List Folders in Classic Outlook

Insert Word Document into Email using VBA

Warn Before Deleting a Contact

Use PowerShell to Delete Attachments

Remove RE:, FWD:, and Other Prefixes from Subject Line

Change the Mailing Address Using PowerShell

Categorize @Mentioned Messages

Send an Email When You Open Outlook

Delete Old Calendar Events using VBA

VBA Basics

How to use the VBA Editor

Work with open item or selected item

Working with All Items in a Folder or Selected Items

VBA and non-default Outlook Folders

Backup and save your Outlook VBA macros

Get text using Left, Right, Mid, Len, InStr

Using Arrays in Outlook macros

Use RegEx to extract message text

Paste clipboard contents

Windows Folder Picker

Custom Forms

Designing Microsoft Outlook Forms

Set a custom form as default

Developer Resources

Developer Resources

Developer Tools

VBOffice.net samples

SlovakTech.com

Outlook MVP David Lee

Repair PST

Convert an OST to PST

Repair damaged PST file

Repair large PST File

Remove password from PST

Merge Two Data Files

Sync & Share Outlook Data

  • Share Calendar & Contacts
  • Synchronize two computers
  • Sync Calendar and Contacts Using Outlook.com
  • Sync Outlook & Android Devices
  • Sync Google Calendar with Outlook
  • Access Folders in Other Users Mailboxes

Diane Poremsky [Outlook MVP]

Make a donation

Mail Tools

Sending and Retrieval Tools

Mass Mail Tools

Compose Tools

Duplicate Remover Tools

Mail Tools for Outlook

Online Services

Calendar Tools

Schedule Management

Calendar Printing Tools

Calendar Reminder Tools

Calendar Dates & Data

Time and Billing Tools

Meeting Productivity Tools

Duplicate Remover Tools

Productivity

Productivity Tools

Automatic Message Processing Tools

Special Function Automatic Processing Tools

Housekeeping and Message Management

Task Tools

Project and Business Management Tools

Choosing the Folder to Save a Sent Message In

Run Rules on messages after reading

Help & Suggestions

Submit Outlook Feature Requests

Slipstick Support Services

Buy Microsoft 365 Office Software and Services

Visit Slipstick Forums.

What's New at Slipstick.com

Home | Outlook User | Exchange Administrator | Office 365 | Outlook.com | Outlook Developer
Outlook for Mac | Common Problems | Utilities & Addins | Tutorials
Outlook & iCloud Issues | Outlook Apps
EMO Archives | About Slipstick | Slipstick Forums
Submit New or Updated Outlook and Exchange Server Utilities

Send comments using our Feedback page
Copyright © 2025 Slipstick Systems. All rights reserved.
Slipstick Systems is not affiliated with Microsoft Corporation.

wpDiscuz

Sign up for Exchange Messaging Outlook

Our weekly Outlook & Exchange newsletter (bi-weekly during the summer)






Please note: If you subscribed to Exchange Messaging Outlook before August 2019, please re-subscribe.

Never see this message again.

You are going to send email to

Move Comment