简体   繁体   English

如何使用 VBA 在 Outlook 中获取通过即时搜索找到的电子邮件?

[英]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 方法的主要好处是:

  • The search is performed in another thread.搜索在另一个线程中执行。 You don't need to run another thread manually since the AdvancedSearch method runs it automatically in the background.您不需要手动运行另一个线程,因为 AdvancedSearch 方法会在后台自动运行它。
  • Possibility to search for any item types: mail, appointment, calendar, notes etc. in any location, ie beyond the scope of a certain folder.可以在任何位置搜索任何项目类型:邮件、约会、日历、便笺等,即超出某个文件夹的 scope。 The Restrict and Find / FindNext methods can be applied to a particular Items collection (see the Items property of the Folder class in Outlook). RestrictFind / FindNext方法可以应用于特定的Items集合(请参阅 Outlook 中Folder class 的Items属性)。
  • Full support for DASL queries (custom properties can be used for searching too).完全支持 DASL 查询(自定义属性也可用于搜索)。 You can read more about this in the Filtering article in MSDN.您可以在 MSDN 中的过滤文章中阅读有关此内容的更多信息。 To improve the search performance, Instant Search keywords can be used if Instant Search is enabled for the store (see the IsInstantSearchEnabled property of the Store class).为了提高搜索性能,如果为商店启用了即时搜索,则可以使用即时搜索关键字(请参阅 Store 类的IsInstantSearchEnabled属性)。
  • You can stop the search process at any moment using the 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM