The present invent
Drug-delivery syst
Q:
How to pass a
In its latest crac
If you're a fan of
A comparison of th
Kinetics and metab
Q:
How to access
/*
* Copyright (
Surgical managemenQ:
What's the quickest way to get a list of all unread Emails from all mailboxes in Outlook 2010?
What's the quickest way to get a list of all unread Emails from all mailboxes in Outlook 2010 ?
The below process is a bit cumbersome and takes about 2-3 minutes.
I've tried the below macro but it does not work on Outlook 2007 and Outlook 2003.
Option Explicit
Sub Test()
Dim objOutlook As Object
Dim objNameSpace As Object
Dim objFolder As Object
Dim objFolderItem As Object
Dim objMailItem As Object
Dim objUnreadEmail As Object
Dim intLoop As Integer
'~~> Specify which outlook version you are using
'Set objOutlook = CreateObject("Outlook.Application")
'~~> Get Outlook Instance
Set objOutlook = CreateObject("Outlook.Application")
'~~> Get the namespaces collection for Outlook
Set objNameSpace = objOutlook.GetNamespace("MAPI")
'~~> Start the loop to get all the folders in the inbox
For intLoop = 1 To objNameSpace.Folders.Count
'~~> Get the next folder in the folder collection
Set objFolder = objNameSpace.Folders.Item(intLoop)
'~~> Process the current Inbox
objFolder.ProcessAllFolders (olFolderInbox)
Next
'~~> Loop through all Inbox Items and delete the read ones
For Each objFolderItem In objFolder.Items
For Each objMailItem In objFolderItem.UnRead
objMailItem.Delete
Next
Next
Set objNameSpace = Nothing
Set objFolder = Nothing
Set objFolderItem = Nothing
Set objMailItem = Nothing
End Sub
A:
I've had good results with something like this:
Sub Test()
Dim objOutlook As Object
Dim objNameSpace As Outlook.NameSpace
Dim folder As Outlook.Folder
Dim subFolders As Outlook.Items
Dim subFolderItem As Object
'~~> Specify which outlook version you are using
Set objOutlook = New Outlook.Application
objOutlook.Session.Logon
Set objNameSpace = objOutlook.GetNamespace("MAPI")
'~~> Specify which folder you are working with
Set folder = objNameSpace.GetDefaultFolder(olFolderInbox)
'~~> Process all sub folders, and unread email items in each sub folder
For Each subFolder In folder.Folders
For Each subFolderItem In subFolder.Items
If subFolderItem.UnRead = True Then
subFolderItem.Delete
End If
Next
Next
Set folder = Nothing
Set subFolders = Nothing
Set subFolderItem = Nothing
End Sub
What it does is that it gets the default folder (usually Inbox), and from there it loops through every subfolder, and then through all of their emails, to check if the email is unread or not.
Edit: In your case, you need to change this line:
For Each objFolderItem In objFolder.Items
To:
For Each objFolderItem In objFolder.Items.Restrict("[UnRead = True]")
This will only select folders and sub-folders that have unread messages.
You can download my open-source code here
A:
While the other answer will work perfectly for most people, you may want to keep in mind that you can have "read" emails in the folder. You might want to do something like:
For i = 1 To objFolder.Items.Count
On Error Resume Next
If Not objFolder.Items(i).RestrictedViews.RestrictionType = 3 Then
' Do your delete code here.
End If
Next
Basically if the item has any restriction, don't even process the item.
A:
I think a quick macro to test, on Outlook 2010:
Option Explicit
Sub GetAllEmailsNotRead()
Dim ns As Outlook.NameSpace
Dim colFolders As Outlook.Folders
Dim fld As Outlook.Folder
Set ns = Application.GetNamespace("MAPI")
Set colFolders = ns.Folders
For Each fld In colFolders
fld.ProcessAllFolders (olFolderInbox)
fld.Items.Restrict("[UnRead]").Delete
Next fld
Set fld = Nothing
Set colFolders = Nothing
Set ns = Nothing
End Sub
EDIT1: Note that the "UnRead" method of Outlook (2003 and 2007+) will not return emails that have been marked as read by the user. If you want to find emails that have not been read by anyone (as far as Outlook knows) then use Outlook.MailItems.Restrict([UnSeen]). See this article for more info.