我有此代码可在我的文件夹中搜索。我确实有一封包含“ sketch”主题的电子邮件,但是VBA找不到它(转到ELSE子句)
谁能告诉我哪里出了问题?
Set olApp = GetObject(, "Outlook.Application")
Set olNs = olApp.GetNamespace("MAPI")
Set olFldr = olNs.GetDefaultFolder(olFolderInbox)
Set olItms = olFldr.Items
Set Mail = olItms.Find("[Subject] = ""*sketch*""") 'Tracking
If Not (Mail Is Nothing) Then
'use mail item here
Else
NoResults.Show
End If
您.Find
无法Items.Find
使用的原因是不支持使用通配符。Items.Find
也不支持搜索部分字符串。因此,要真正找到电子邮件,您需要删除通配符,并将整个字符串包括在搜索条件中。
所以这是您的选择:
如果您知道要查找的完整主题行,请像下面这样修改代码:
Set Mail = olItms.Find("[Subject] = ""This Sketch Email""")
如果您不知道(或不知道)全部主题,则可以遍历收件箱文件夹并搜索部分主题行,如下所示:
未经测试
Sub Search_Inbox()
Dim myOlApp As New Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myInbox As Outlook.MAPIFolder
Dim myitems As Outlook.Items
Dim myitem As Object
Dim Found As Boolean
Set myNameSpace = myOlApp.GetNamespace("MAPI")
Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myitems = myInbox.Items
Found = False
For Each myitem In myitems
If myitem.Class = olMail Then
If InStr(1, myitem.Subject, "sketch") > 0 Then
Debug.Print "Found"
Found = True
End If
End If
Next myitem
'If the subject isn't found:
If Not Found Then
NoResults.Show
End If
myOlApp.Quit
Set myOlApp = Nothing
End Sub
希望有帮助!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句