[英]How to fetch emails found by instant search in Outlook using VBA?
I use VBA to find email(s) by content of it's pdf attachment.我使用 VBA 按其 pdf 附件的内容查找电子邮件。
From what I understood, the only method without opening each email is Instant search.据我了解,不打开每个 email 的唯一方法是即时搜索。
I find the emails I look for.我找到了我要查找的电子邮件。 How do I work with emails that show as the result?如何处理显示为结果的电子邮件?
I didn't find how to save them to collection or something.我没有找到如何将它们保存到收藏或其他东西。
My search code:我的搜索代码:
Sub Search_Email()
Dim ol As Outlook.Application
Dim ns As Outlook.Namespace
Dim olFolder As Outlook.MAPIFolder
Dim strFilter, txtSearch As String
Set ol = New Outlook.Application
Set ns = ol.GetNamespace("MAPI")
Set olFolder = ns.Folders("RS Cash Application Inbox").Folders("Remittances")
Set ol.ActiveExplorer.CurrentFolder = olFolder
strFilter = "5860626494"
txtSearch = "attachment:" & strFilter
ol.ActiveExplorer.Search txtSearch, olSearchScopeCurrentFolder
End Sub
Result of the instant search.即时搜索的结果。 I need to download attachments from both displayed emails.我需要从两个显示的电子邮件中下载附件。
When calling Search
, the query is run in the user interface, and there is no programmatic mechanism to obtain the search results.调用Search
时,查询是在用户界面中运行的,并且没有程序化机制来获取搜索结果。 Also the Search
method does not provide a callback to enable the developer to determine when the search is complete.此外, Search
方法不提供回调以使开发人员能够确定搜索何时完成。
Instead, I'd suggest using the AdvancedSearch method of the Application
class which provides the callback to get the results after.相反,我建议使用Application
class 的AdvancedSearch方法,该方法提供回调以获取之后的结果。 The key benefits of using the AdvancedSearch method in Outlook are:在 Outlook 中使用 AdvancedSearch 方法的主要好处是:
Restrict
and Find
/ FindNext
methods can be applied to a particular Items
collection (see the Items
property of the Folder
class in Outlook). Restrict
和Find
/ FindNext
方法可以应用于特定的Items
集合(请参阅 Outlook 中Folder
class 的Items
属性)。IsInstantSearchEnabled
property of the Store class).为了提高搜索性能,如果为商店启用了即时搜索,则可以使用即时搜索关键字(请参阅 Store 类的IsInstantSearchEnabled
属性)。Stop
method of the Search class.您可以随时使用 Search class 的Stop
方法停止搜索过程。 To retrieve the search results you need to handle the AdvancedSearchComplete
event which is used to return the object that was created by the AdvancedSearch
method.要检索搜索结果,您需要处理AdvancedSearchComplete
事件,该事件用于返回由AdvancedSearch
方法创建的 object。 This event only fires when the AdvancedSearch
method is executed programmatically.此事件仅在以编程方式执行AdvancedSearch
方法时触发。
Public blnSearchComp As Boolean
Private Sub Application_AdvancedSearchComplete(ByVal SearchObject As Search)
MsgBox "The AdvancedSearchComplete Event fired."
blnSearchComp = True
End Sub
Sub TestAdvancedSearchComplete()
Dim sch As Outlook.Search
Dim rsts As Outlook.Results
Dim i As Integer
blnSearchComp = False
Const strF As String = "urn:schemas:mailheader:subject = 'Test'"
Const strS As String = "Inbox"
Set sch = Application.AdvancedSearch(strS, strF)
While blnSearchComp = False
DoEvents
Wend
Set rsts = sch.Results
For i = 1 To rsts.Count
MsgBox rsts.Item(i).SenderName
Next
End Sub
After using the Search
on the Outlook UI you can get access to the CurrentView
by accessing corresponding properties on the Explorer
object.在 Outlook UI 上使用Search
后,您可以通过访问Explorer
object 上的相应属性来访问CurrentView
。 The View
class provides the Filter property which returns a string value that represents the filter for a view. View
class 提供了Filter属性,该属性返回表示视图过滤器的字符串值。 The value of this property is a string, in DAV Searching and Locating (DASL) syntax, that represents the current filter for the view.此属性的值是一个字符串,采用 DAV 搜索和定位 (DASL) 语法,表示视图的当前过滤器。 So, you can use it for the AdvancedSearch
search string.因此,您可以将其用于AdvancedSearch
搜索字符串。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.