问题
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"
The list view changes to
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
回答1:
There are two possible ways to show search results in Outlook:
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:- 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.
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 theSave
method on theSearch
object in theAdvanvedSearchComplete
event handler.- The search is performed in another thread. You don’t need to run another thread manually since the
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
来源:https://stackoverflow.com/questions/50139841/programmatically-open-search-view