Search code examples
vbasearchoutlookdisplay

Show or display items collection or search object in ActiveExplorer


I can capture a collection of items to a variable by Items.Restrict or a Search Object by AdvancedSearch ... how do I display the Items or Search in the ActiveExplorer??

Where objSearch is a Search Object

Set objSearch = objOL.AdvancedSearch(strScope, strSearch, True, "PracticeSearch")

Or where rtrndItems is an Items Object

Set rtrndItems = myItems.Restrict(strSearch)

Thanks for any help!!


Solution

  • All these methods are designed to be run programmatically without any UI feedback. The best what you could do is to save the AdvancedSearch results into a search folder in Outlook. The Search.Save method saves the search results to a Search Folder. For example:

    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 
     
     sch.Save("Subject Test") 
     
    End Sub
    

    To search items in Outlook with any UI feedback involved you need to use the Explorer.Search method which performs a Microsoft Instant Search on the current folder displayed in the Explorer using the given Query

    Here is what MSDN states for the Search method:

    The functionality of Explorer.Search is analogous to the Search button in Instant Search. It behaves as if the user has typed the query string in the Instant Search user interface and then clicked Search. When calling Search, the query is run in the user interface, and there is no programmatic mechanism to obtain the search results. For more information on Instant Search, query for "Instant Search" in the Outlook Help.

    The Search method does not provide a callback to enable the developer to determine when the search is complete.