0

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" enter image description here

The list view changes to

enter image description here

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

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
Miserable Variable
  • 28,432
  • 15
  • 72
  • 133

1 Answers1

0

There are two possible ways to show search results in Outlook:

  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.

    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.

  2. 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

Eugene Astafiev
  • 47,483
  • 3
  • 24
  • 45
  • The Outlook object model is the same for VBA and VB.NET. – Eugene Astafiev May 03 '18 at 00:13
  • True, but lacks Try Catch Finally. – Ryan Wildry May 03 '18 at 00:19
  • 1
    _I think OP may recognize helpful pieces of code_ -- You could have written this in C# or Python with the same hope. Also, you are using .NET classes which are not available in VBA, [as someone has already discovered](https://stackoverflow.com/questions/51602735/xmldocument-in-outlook-vba?noredirect=1&lq=1). It's unfortunate, because the first half of your answer provides some valuable information. – Zev Spitz Jul 30 '18 at 23:07
  • I have accepted this answer as I was finally able to adapt it for use with VBA (that someone that @ZevSpitz refers to is me :)). I do believe it was much easier for me to make progress with an example in VB.NET, rather then Python. – Miserable Variable Aug 02 '18 at 15:22