PDA

View Full Version : [SOLVED:] Outlook Find Emails with multiple criteria



lawsonbooth
08-01-2016, 09:36 AM
I have searched the forum and cannot find an answer.:bug:

I am pretty good with VBA, but I do not understand Outlook VBA. I need help with the following problem:

Inbox has 148 items.

Need to pull oldest email with the word “Urgent” or “Rush” or “Escalation” anywhere in the subject line and the email is unread.

Outlook 2010 on Windows 7.

Any help and guidance will be greatly appreciated.

Lawson

gmayor
08-01-2016, 09:30 PM
In that case you need to sort the inbox message collection in reverse date order and process the resulting list e.g.


Dim olItems As Outlook.Items
Dim olItem As Outlook.MailItem
Dim i As Long

'add the inbox content to the Items collection
Set olItems = Session.GetDefaultFolder(olFolderInbox).Items
'Sort the collection in reverse order
olItems.Sort "[Received]", False
'Process each message from last to first
For i = 1 To olItems.Count
Set olItem = olItems(i)
'Check if the message is unread
If olItem.UnRead = True Then
'Check if the subject contains one of the key words
If InStr(1, olItem.subject, "Urgent") > 0 Or _
InStr(1, olItem.subject, "Rush") > 0 Or _
InStr(1, olItem.subject, "Escalation") > 0 Then
'do something with olitem here
End If
End If
Next i

lawsonbooth
08-02-2016, 05:17 AM
This works great! Thanks Graham for your quick response and help.

Lawson