1

The code found in this thread works fine for single email results: Excel VBA for searching in mails of Outlook, But it only returns the latest email.

Is it possible to adjust the code to display more than 1 result?

The code that I have from the thread is:

Option Explicit

Public Sub search_outlook()

    Dim outlookapp
    Dim olNs As Outlook.Namespace
    Dim Fldr As Outlook.MAPIFolder
    Dim olMail As Variant
    Dim myTasks

    Dim projIDsearch As String

    projIDsearch = ActiveCell.Cells(1, 4)
    Set outlookapp = CreateObject("Outlook.Application")

    'Set outlookapp = New Outlook.Application
    Set olNs = outlookapp.GetNamespace("MAPI")
    Set Fldr = olNs.GetDefaultFolder(olFolderInbox).Folders("ExemptionReview")
    Set myTasks = Fldr.Items

    For Each olMail In myTasks
        If (InStr(1, olMail.Subject, projIDsearch, vbTextCompare) > 0) Then
            olMail.Display
            Exit For
        End If
    Next

End Sub
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
wh3resmycar2
  • 191
  • 1
  • 13

1 Answers1

1

Remove the Exit For. This drops out of the loop when a match is found:

olMail.Display  
Exit For       '<Remove this
CLR
  • 11,284
  • 1
  • 11
  • 29