Excel VBAsearch邮件的Outlook

我正在开发一个项目,里面有一个Account ID的列表,我正在做的是创build一个与Outlook接口的macros,在特定条件下search我的收件箱中的任何电子邮件,然后返回“ Y“或”N“,如果find,发送邮件的人和发送的时间。 以下是我正在使用的代码; 我需要macros来search电子邮件正文而不是主题行。 当我将[主体]replace为[主体]时,macros运行没有错误,但没有返回邮件(我放置了几个testing邮件来捕捉它)。 我正在运行Excel和Outlook 2007,并且已经引用了VBA中的MS 12.0 Excel和Outlook库。

Sub Work_with_Outlook() Set outlookApp = CreateObject("Outlook.Application") Dim olNs As Outlook.Namespace Dim Fldr As Outlook.MAPIFolder Dim olMail As Variant Dim sir() As String Set outlookApp = New Outlook.Application Set olNs = outlookApp.GetNamespace("MAPI") Set Fldr = olNs.GetDefaultFolder(olFolderInbox) Set myTasks = Fldr.Items Set olMail = myTasks.Find("[Subject] = ""123456""") If Not (olMail Is Nothing) Then olMail.Display End If End Sub 

您不能在Find(Filter)中使用Body,请参阅Items.Find方法(Outlook) ,作为解决方法,您可以使用VBAstringsearchfunction:

 Sub sofWorkWithOutlook20082550() Dim outlookApp Dim olNs As Outlook.Namespace Dim Fldr As Outlook.MAPIFolder Dim olMail As Variant Dim myTasks Dim sir() As String 'Set outlookApp = New Outlook.Application Set outlookApp = CreateObject("Outlook.Application") Set olNs = outlookApp.GetNamespace("MAPI") Set Fldr = olNs.GetDefaultFolder(olFolderInbox) Set myTasks = Fldr.Items ' 'Set olMail = myTasks.Find("[Subject] = ""123456""") ' For Each olMail In myTasks ' If (InStr(1, olMail.Body, "My-Text-to-Search", vbTextCompare) > 0) Then olMail.Display Exit For End If Next End Sub