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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:41:45  来源:igfitidea点击:

VBA Search in Outlook

vbaoutlook-vba

提问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 .Findisn't working is because Items.Finddoesn't support the use of wildcards. Items.Findalso 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!

希望有帮助!