Outlook 中的 VBA 搜索
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21549938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
VBA Search in Outlook
提问by AndroidDev
I have this code to search in my folder. I do have a e-mail with the "sketch" subject, but VBA is not finding it (it goes to the ELSE clause)
我有这个代码可以在我的文件夹中搜索。我确实有一封带有“草图”主题的电子邮件,但 VBA 没有找到它(它转到了 ELSE 子句)
Can anybody tell what is wrong ?
谁能告诉我出了什么问题?
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
回答by jeffld
Here is a way to do the search using Items Restrict.
这是一种使用 Items Restrict 进行搜索的方法。
This runs fast and you do not need to loop through the items to find the items that match the search criteria.
这运行得很快,您不需要遍历项目来查找与搜索条件匹配的项目。
Sub Search_Inbox()
Dim myOlApp As New Outlook.Application
Dim objNamespace As Outlook.NameSpace
Dim objFolder As Outlook.MAPIFolder
Dim filteredItems As Outlook.Items
Dim itm As Object
Dim Found As Boolean
Dim strFilter As String
Set objNamespace = myOlApp.GetNamespace("MAPI")
Set objFolder = objNamespace.GetDefaultFolder(olFolderInbox)
strFilter = "@SQL=" & Chr(34) & "urn:schemas:httpmail:subject" & Chr(34) & " like '%sketch%'"
Set filteredItems = objFolder.Items.Restrict(strFilter)
If filteredItems.Count = 0 Then
Debug.Print "No emails found"
Found = False
Else
Found = True
' this loop is optional, it displays the list of emails by subject.
For Each itm In filteredItems
Debug.Print itm.Subject
Next
End If
'If the subject isn't found:
If Not Found Then
'NoResults.Show
Else
Debug.Print "Found " & filteredItems.Count & " items."
End If
'myOlApp.Quit
Set myOlApp = Nothing
End Sub
回答by ARich
The reason your .Find
isn't working is because Items.Find
doesn't support the use of wildcards. Items.Find
also doesn't support searching partial strings. So to actually find the email, you'd need to remove the wildcards and include the entire string in your search criteria.
您.Find
不工作的原因是因为Items.Find
不支持使用通配符。Items.Find
也不支持搜索部分字符串。因此,要真正找到电子邮件,您需要删除通配符并在搜索条件中包含整个字符串。
So here are your options:
所以这里有你的选择:
If you know the full subject line you're looking for, modify your code like so:
如果您知道要查找的完整主题行,请像这样修改您的代码:
Set Mail = olItms.Find("[Subject] = ""This Sketch Email""")
If you don't (or won't) know the full subject, you can loop through your inbox folder and search for a partial subject line like so:
如果您不(或不会)知道完整的主题,您可以循环浏览收件箱文件夹并搜索部分主题行,如下所示:
Untested
未经测试
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
Hope that helps!
希望有帮助!