此线程中的代码适用于单个电子邮件结果:Excel VBA for searching in mails of Outlook,但它只返回最新的电子邮件。
是否可以调整代码以显示多于1个结果?
我从该主题获得的代码是:
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
答案 0 :(得分:1)
删除Exit For
。当找到匹配时,这会退出循环:
olMail.Display
Exit For '<Remove this