以编程方式打开搜索视图 [英] programmatically open search view
问题描述
我正在寻找一种以编程方式在Outlook中针对指定条件打开搜索视图的方法.我看到了其他问题,例如 Outlook中的VBA搜索,但它们与通过编程方式获取搜索结果有关.
I am looking for a way to open a search view in outlook for a specified criteria programmatically. I saw other questions such as VBA Search in Outlook, but they are about retrieving search results programmatically.
我本质上是在寻找与功能区选项消息/编辑/相关"相同的功能
I am essentially looking for the same functionality as the Ribbon option Message/Editing/Related:
当我通过双击某条消息打开一条消息,然后单击此对话中的消息"时,
When I open a message by double-clicking on a message and then click on "Messages in this Conversation"
列表视图更改为
我希望能够使用VBA宏执行相同的操作,但无需打开消息并使用不同的搜索条件
I would like to be able to do the same thing using a VBA Macro but without opening the message and with a different search criteria
推荐答案
在Outlook中显示搜索结果的方法有两种:
There are two possible ways to show search results in Outlook:
-
Outlook对象模型中Application类的
AdvancedSearch
方法允许在Outlook中的多个文件夹中执行搜索.在Outlook中使用AdvancedSearch
方法的主要好处是:
The
AdvancedSearch
method of the Application class from the Outlook object model allows to perform a search in multiple folders in Outlook. The key benefits of using theAdvancedSearch
method in Outlook are:
- 搜索是在另一个线程中执行的.您无需手动运行另一个线程,因为
AdvancedSearch
方法会在后台自动运行它. - 可以在任何位置(即超出某个文件夹的范围)搜索任何项目类型:邮件,约会,日历,便笺等.可以将Restrict和
Find
/FindNext
方法应用于特定的Items集合(请参阅Outlook中Folder类的Items属性). - 完全支持DASL查询(自定义属性也可用于搜索).您可以在过滤 MSDN中的文章.为了提高搜索性能,如果为商店启用了即时搜索",则可以使用即时搜索"关键字(请参阅
Store
类的IsInstantSearchEnabled
属性). - 最后,您可以随时使用Search类的Stop方法停止搜索过程.
- 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. - Possibility to search for any item types: mail, appointment, calendar, notes etc. in any location, i.e. beyond the scope of a certain folder. The Restrict and
Find
/FindNext
methods can be applied to a particular Items collection (see the Items property of the Folder class in Outlook). - Full support for DASL queries (custom properties can be used for searching too). You can read more about this in the Filtering article in 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 theStore
class). - Finally, you can stop the search process at any moment using the Stop method of the Search class.
诚然,Search
类允许您将搜索结果保存在搜索文件夹中(实际上,它不包含任何项目,仅引用作用域文件夹中的项目).您只需要在AdvanvedSearchComplete
事件处理程序中的Search
对象上调用Save
方法.
Admittedly, the Search
class allows you to save the results of searching in a search folder (actually, it doesn’t contain any items, only references to items from the scope folders). You just need to call the Save
method on the Search
object in the AdvanvedSearchComplete
event handler.
在Outlook中自定义Folder或Explorer对象的当前视图. CurrentView 属性返回一个View
类的实例.请参见如何:使用View.XML属性过滤Outlook项目以获取更多信息.
Customize the current view of the Folder or Explorer objects in Outlook. The CurrentView property returns an instance of the View
class. See HowTo: Use the View.XML property to filter Outlook items for more information.
Dim sFilter As String
Dim CurrentExplorer As Outlook.Explorer = Nothing
Dim CurrentView As Outlook.View = Nothing
Dim CurrentXML As XmlDocument = New XmlDocument
Dim CurrentFilterNodes, CurrentViewNodes As XmlNodeList
Dim CurrentFilterNode, CurrentParentNode As XmlNode
If TextBox1.Text.Length > 0 Then
If rbtSubject.Checked Then
sFilter = "urn:schemas:httpmail:subject LIKE '%" + _
TextBox1.Text.Trim + "%'"
Else
sFilter = "urn:schemas:httpmail:textdescription LIKE '%" + _
TextBox1.Text.Trim + "%'"
End If
CurrentExplorer = TryCast(ExplorerObj, Outlook.Explorer)
If (CurrentExplorer IsNot Nothing) Then
CurrentView = CurrentExplorer.CurrentView
If (CurrentView IsNot Nothing) Then
Try
CurrentXML.LoadXml(CurrentView.XML)
CurrentFilterNodes = _
CurrentXML.GetElementsByTagName("filter")
If CurrentFilterNodes.Count > 0 Then
For y As Integer = 0 _
To CurrentFilterNodes.Count - 1
CurrentFilterNode = CurrentFilterNodes(y)
If CurrentFilterNode.HasChildNodes Then
For i As Integer = _
CurrentFilterNode.ChildNodes.Count - 1 _
To 0 Step -1
CurrentFilterNode.RemoveChild( _
CurrentFilterNode.ChildNodes(i))
Next
End If
Next
CurrentFilterNode = CurrentFilterNodes(0)
CurrentFilterNode.AppendChild( _
CurrentXML.CreateTextNode(sFilter))
Else
CurrentViewNodes = CurrentXML. _
GetElementsByTagName("view")
If CurrentViewNodes IsNot Nothing Then
CurrentParentNode = CurrentViewNodes(0)
CurrentFilterNode = CurrentXML. _
CreateElement("filter")
CurrentParentNode.AppendChild( _
CurrentFilterNode)
CurrentFilterNode.AppendChild( _
CurrentXML.CreateTextNode(sFilter))
End If
End If
CurrentView.XML = CurrentXML.InnerXml
CurrentView.Apply()
Finally
Marshal.ReleaseComObject(CurrentView)
End Try
End If
End If
End If
这篇关于以编程方式打开搜索视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!