我正在开发一个项目,其中有一个帐户ID列表,我正在尝试做的是创建一个与Outlook交互的宏,在我的收件箱中搜索具有特定条件的任何电子邮件,以及如果找到,则返回“Y”或“N”,如果找到,则发送电子邮件的人和发送的时间。下面是我正在使用的代码;我需要宏来搜索电子邮件的正文而不是主题行。当我用[Body]替换[Body]时,宏运行没有错误,但没有返回电子邮件(我放置了几个测试电子邮件以便捕获)。我正在运行Excel和Outlook 2007,并且已经参考了MS 12.0 Excel& VBA中的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
答案 0 :(得分:10)
您不能在查找(过滤器)中使用Body,请参阅Items.Find Method (Outlook), 作为解决方法,您可以使用VBA字符串搜索功能:
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