以编程方式打开搜索视图 [英] programmatically open search view

查看:87
本文介绍了以编程方式打开搜索视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找一种以编程方式在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:

  1. Outlook对象模型中Application类的AdvancedSearch方法允许在Outlook中的多个文件夹中执行搜索.在Outlook中使用AdvancedSearch方法的主要好处是:

  1. 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 the AdvancedSearch method in Outlook are:

  • 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 the Store 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆