Outlook 2016: Searching in all (+public) folders

Status
Not open for further replies.

cramocairyk

New Member
Outlook version
Outlook 2016 64 bit
Email Account
Exchange Server
I can't tell you how frustrated I was that Microsoft disabled the Windows Search feature in Outlook 2016 !

The ability to perform a single search in all of my 200+ public folders (and in my Inbox), with the aggregate results being displayed in a single window, was invaluable !

This is my first attempt at restoring that functionality in Outlook 2016 (tested with Office 365 ProPlus on 64-bit Windows 7).

Although the search results for the Inbox (and its sub-folders) all appear in the same results window, I wasn't able to find a way to get the search results from Public sub-folders to aggregate into a single results window, so the macro below creates a separate search results window for each Public sub-folder having e-mails that satisfy your search criterion.

Don't panic if it takes a long (long !) time for the results windows to start appearing ! The search operations (apparently) take precedence over window management, so the nested folder searches will all complete before any of the windows will start to appear (with the possible exception of the search results window for the Inbox and its sub-folders)

I can't take credit for *everything* you see below - I borrowed bits and pieces of it from the best on the 'net ! Many thanks to those who took the time to post their samples !

Good luck with it ... feel free to let me know what you think of it, but I make no promises of proper support !

BTW, this macro will try to find a View called "Search Results" - if it exists, it will use that View to control the format of each results window, otherwise it will default to the View that was currently selected in the active Outlook window prior to running the macro. So if you want to customize the look and feel of the result windows, just create a view by that name and tailor it to your liking, then perform your search.

Code:
Private Sub RecursiveFolderSearch(ByRef objParentFolder As Outlook.MAPIFolder, ByVal strViewName As String, ByVal strDASLFilter As String)

    On Error GoTo Err_SearchFolder

    '
    ' If folder has items other than just sub-folders
    '

    If objParentFolder.Items.Count Then

        '
        ' Search this sub-folder
        '

        Dim strScope As String
        'strScope = "'Inbox'"                                                                           ' works but Results.Count is 0 -> results can only be saved
        'strScope = "'Inbox','Sent Items'"                                                              ' works but Results.Count is 0 -> results can only be saved
        'strScope = "'" & objMyPublicFolder.FolderPath & "'"                                            ' works and Results.Count <> 0 -> but results cannot be saved
        'strScope = "'Inbox','" & objMyPublicFolder.FolderPath & "'"                                    ' doesn't work
        'strScope = "'" & objMyPublicFolder.FolderPath & "','" & objMyPublicFolderToo.FolderPath & "'"  ' doesn't work
        strScope = "'" & objParentFolder.FolderPath & "'"

        Dim objSearch As Search
        Set objSearch = Application.AdvancedSearch(Scope:=strScope, Filter:=strDASLFilter, SearchSubFolders:=False, Tag:="SearchFolder")

        If objSearch.Results.Count Then

            '
            ' Display the results in a separate Outlook Explorer window
            '

            Dim objExplorer As Outlook.Explorer
            Set objExplorer = Application.Explorers.Add(objParentFolder, olFolderDisplayNormal)
            Call objExplorer.Display

            Dim objCurrentView As Outlook.View
            Set objCurrentView = objExplorer.CurrentFolder.Views.Item("Search Results")
            If objCurrentView Is Nothing Then
                Set objCurrentView = objExplorer.CurrentFolder.Views.Item(strViewName)
            End If

            objCurrentView.Filter = strDASLFilter

            On Error GoTo Err_Ignore
            objCurrentView.Apply
Err_Ignore:
            On Error GoTo Err_SearchFolder

            Set objCurrentView = Nothing
            Set objExplorer = Nothing

        End If

        Set objSearch = Nothing

    End If

    '
    ' Recurse through sub-folders
    '
   
    Dim objFolder As Outlook.MAPIFolder
    For Each objFolder In objParentFolder.Folders
        Call RecursiveFolderSearch(objFolder, strViewName, strDASLFilter)
    Next
    Set objFolder = Nothing

    Exit Sub

Err_SearchFolder:

    MsgBox "Error # " & Err & " : " & Error(Err)

End Sub


Public Sub SearchInAllFolders()

    On Error GoTo Err_SearchFolder

    '
    ' Collect information
    '

    Dim objApplication As Outlook.Application
    Set objApplication = CreateObject("Outlook.Application")

    Dim objNamespace As Outlook.NameSpace
    Set objNamespace = objApplication.GetNamespace("MAPI")

    Dim strViewName As String
    strViewName = Application.ActiveExplorer.CurrentView.Name
   
    Dim Account As String
    Account = objNamespace.Accounts(1).SmtpAddress

    '
    ' Input search string
    '

    Dim strFilter As String
    strFilter = InputBox("Enter Search String:", "Search in all files")
    If strFilter = "" Then
        Exit Sub
    End If

    Dim strDASLFilter As String
    strDASLFilter = """urn:schemas:httpmail:fromname"" LIKE '%" & strFilter & "%' " + _
    "OR ""urn:schemas:httpmail:textdescription"" LIKE '%" & strFilter & "%' " + _
    "OR ""urn:schemas:httpmail:displaycc"" LIKE '%" & strFilter & "%' " + _
    "OR ""urn:schemas:httpmail:displayto"" LIKE '%" & strFilter & "%' " + _
    "OR ""urn:schemas:httpmail:subject"" LIKE '%" & strFilter & "%' " + _
    "OR ""urn:schemas:httpmail:thread-topic"" LIKE '%" & strFilter & "%' " + _
    "OR ""http://schemas.microsoft.com/mapi/received_by_name"" LIKE '%" & strFilter & "%' " + _
    "OR ""http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/8586001f"" LIKE '%" & strFilter & "%' " + _
    "OR ""http://schemas.microsoft.com/mapi/id/{00062008-0000-0000-C000-000000000046}/85a4001f"" LIKE '%" & strFilter & "%' " + _
    "OR ""http://schemas.microsoft.com/mapi/id/{00062041-0000-0000-C000-000000000046}/8904001f"" LIKE '%" & strFilter & "%' " + "OR ""http://schemas.microsoft.com/mapi/proptag/0x0e03001f"" LIKE '%" & strFilter & "%' " + _
    "OR ""http://schemas.microsoft.com/mapi/proptag/0x0e04001f"" LIKE '%" & strFilter & "%' " + _
    "OR ""http://schemas.microsoft.com/mapi/proptag/0x0042001f"" LIKE '%" & strFilter & "%' " + "OR ""http://schemas.microsoft.com/mapi/proptag/0x0044001f"" LIKE '%" & strFilter & "%' " + _
    "OR ""http://schemas.microsoft.com/mapi/proptag/0x0065001f"" LIKE '%" & strFilter & "%' "

    '
    ' Search Inbox and its sub-folders
    '

    Dim strScope As String
    strScope = "Inbox"

    Dim objSearch As Search
    Set objSearch = Application.AdvancedSearch(Scope:=strScope, Filter:=strDASLFilter, SearchSubFolders:=True, Tag:="SearchFolder")

    '
    ' Save the search results to a SearchFolder
    '

    objSearch.Save (strFilter)
    Set objSearch = Nothing

    '
    ' Display the SearchFolder in a separate Outlook Explorer window
    '

    Dim objStore As Outlook.Store
    Set objStore = Application.Session.Stores.Item(Account)
    If objStore Is Nothing Then GoTo SearchPublicFolders

    Dim objSearchFolders As Outlook.Folders
    Set objSearchFolders = objStore.GetSearchFolders
    If objSearchFolders Is Nothing Then GoTo SearchPublicFolders

    Dim objSearchFolder As Outlook.Folder
    Set objSearchFolder = objSearchFolders.Item(strFilter)
    If objSearchFolder Is Nothing Then GoTo SearchPublicFolders
   
    Dim objExplorer As Outlook.Explorer
    Set objExplorer = Application.Explorers.Add(objSearchFolder, olFolderDisplayNormal)
    Call objExplorer.Display

    Dim objCurrentView As Outlook.View
    Set objCurrentView = objExplorer.CurrentFolder.Views.Item("Search Results")
    If objCurrentView Is Nothing Then
        Set objCurrentView = objExplorer.CurrentFolder.Views.Item(strViewName)
    End If
   
    On Error GoTo Err_Ignore
    objCurrentView.Apply
Err_Ignore:
    On Error GoTo Err_SearchFolder

    '
    ' Search Public folder 'Favorites' and its sub-folders
    '

SearchPublicFolders:

    Dim objPublicFolders As Outlook.MAPIFolder
    Set objPublicFolders = objNamespace.Folders("Public Folders - " & Account)

    Dim objFavoritesFolder As Outlook.MAPIFolder
    Set objFavoritesFolder = objPublicFolders.Folders("Favorites")

    Call RecursiveFolderSearch(objFavoritesFolder, strViewName, strDASLFilter)

    GoTo Cleanup

Err_SearchFolder:

    MsgBox "Error # " & Err & " : " & Error(Err)

Cleanup:

    Set objFavoritesFolder = Nothing
    Set objPublicFolders = Nothing
    Set objCurrentView = Nothing
    Set objExplorer = Nothing
    Set objSearchFolder = Nothing
    Set objSearchFolders = Nothing
    Set objStore = Nothing
    Set objSearch = Nothing
    Set objNamespace = Nothing
    Set objApplication = Nothing

End Sub
 
One more thing: if your search criterion was too general, a great many windows will get created ! In Windows 7, float your mouse pointer over the Outlook icon in the task bar - this should show you a scrollable list of all of the (many !) search results windows that were created (one for each Public sub-folder) - from that list, you can choose to view a particular window based on the relevance of the folder names to the subject of your search. I would recommend closing all Outlook windows (by right-clicking where you were floating, and choosing "Close all windows") after you're finished with the search results, instead of trying to close each and every results window ... then just re-start Outlook to open your main Outlook window once again.

And when I say all Public folders, what I really mean is all of the Public folders under the Public *Favorites* folder ... so if you want to include specific public folders (and sub-folders) in your search, tailor your Public *Favorites* folder to contain the list of top-level Public folders to search within.
 
Sorry, one more thing: you may get a popup that says "The operation failed" - just click on OK to make it go away - I don't know the consequences of this error yet, but it doesn't (seem to) stop the search results from appearing ... maybe in the worst case one of your Public folders isn't getting searched ? I don't know ... let me know ! Thanks.
 
Just noticed that if you do the bit about creating a View called "Search Results", the macro will set the default view for any folder in which it found e-mails matching your search criterion to "Search Results", and this persists even after you have closed all search results windows (i.e. if you click on one of these folders at some point in the future, the "Search Results" View will still be the default view for the folder, thus you won't see all e-mail items in that folder anymore, you'll see only the e-mails that match the previous search's criterion). To go back to seeing all e-mails in that folder, switch the folder view back to any other of your Views.

Not ideal, I know.

I'm thinking a better approach would be to generate a VBA form containing a list of folders in which e-mails matching the search criterion were found ... clicking on a folder in this list would bring up an (Outlook) Explorer window that would display the filtered results, but closing this window (or the VBA form) would undo the filtering of results. This would simultaneously fix the many-windows problem and this persistent filter problem ...
 
Also noticed that the macro doesn't work for other languages (e.g. French) -> it's because the names "Public Folders" and "Favorites", which are hard-coded in the macro, are called something else in other languages. The quick fix is to replace these names in the macro with the equivalent names for these that you see in your Outlook window
 
Apologies for asking a really stupid question but I have very limited experience with VBScripts as such, but how do I run the above script? Do I run it via CScript in the command prompt or elsewhere?
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
J Searching message folder in Outlook 2016 Using Outlook 5
farrissf Outlook 2016 Optimizing Email Searches in Outlook 2016: Seeking Insights on Quick Search vs Advanced Search Features Using Outlook 0
S Outlook 2016 and Acrobat PDFMaker Office COM Addin Using Outlook 0
W Outlook 2016 MSI - Possible to make work with O365 modern Auth & Win7? Using Outlook 4
J Outlook 2016 Trying to get Outlook 2016 to work with Office 365 Using Outlook 0
W Transfer Outlook 2016 autocomplete file to Outlook 2007 Using Outlook 1
J Outlook 2016 Can't display some embedded HTML images in Outlook 2016 Using Outlook 2
J Outlook 365 Outlook 2016/365 Contacts Lose Info when Favorited to the To-Do Bar Using Outlook 2
O Moving "tasks" to inbox in Outlook 2016 Using Outlook 1
Z Remove GMAIL IMAP account from Outlook 2016 Using Outlook 2
P Prevent Outlook 2016 from using DASL filter Using Outlook 4
bhamberg Shortcuts in Folder Pane (Outlook 2016) Using Outlook 19
G Outlook 2016: Want IMAP Data Files on My D: Drive and Not C: Drive Using Outlook 1
D Outlook 2016 64bit, Cannot Save in 'HTML', format Using Outlook 1
P Color Code or highlight folders in Outlook 2016 Using Outlook 2
B Outlook 2016 Unable to view images or logos on the outlook 2016 emails the same html code works well when i use outlook 2010 Using Outlook 0
B vBA for exporting excel file from outlook 2016 Outlook VBA and Custom Forms 3
M Issue transferring Outlook 2016 email accounts & settings onto new computer Using Outlook 8
glnz Can Word Normal.dotm interfere with Outlook? Office 2016 Using Outlook 5
D Outlook 2016 and Earlier with Office365 in 2021 Using Outlook 3
B Outlook 2016 Retail C2R keeps logging since update? Using Outlook 0
V Outlook 2016 Does Outlook-2016 (64 bit) work with iCloud for Windows ? Using Outlook 5
R Microsoft Outlook 2016 - Gmail not sending, asks for password for SMTP, tried different ports Using Outlook 23
V Outlook 2016 Outlook-2016 and iCloud for Windows - Problems Using Outlook 11
U Outlook 2016 Outlook 2016 sender name Using Outlook 1
S Outlook (2016 32bit; Gmail IMAP) - Save sent message to Outllook Folder Outlook VBA and Custom Forms 0
A Backup Email Accounts On OutLook For Mac 2016 (Microsoft 365 subscription version) Using Outlook 0
M Office 2016 Outlook is forgetting passwords Using Outlook 15
E Can one still buy Outlook (or Office) 2016? Using Outlook 6
I Error saving screenshots in a custom form in outlook 2016, outlook 365 - ok in outlook 2013, outlook 2010 Outlook VBA and Custom Forms 5
R seperate read layout to design in outlook 2016..Help!! Outlook VBA and Custom Forms 3
GregS Why is Outlook 2016 mail in Outlook.com? Using Outlook 1
G Recurring tasks break links Outlook 2016 Using Outlook 5
A How to get body of all emails in outlook 2016 to view in blue color Using Outlook 1
J Edit auto-complete list in Outlook 2016+/365? Using Outlook 0
D iCloud Add-in not working in Outlook 2013 and Outlook 2016 After Windows Upgrade & iCloud Upgrade Using Outlook 2
GregS Outlook 2016 iPhone won't download Outlook 2016 Mail. Using Outlook 0
E How to display "Change Folder" in Change Default Email Delivery Location in Exchange Outlook 2016 Using Outlook 1
A How to open a specific link automatically with outlook 2016 Outlook VBA and Custom Forms 6
V Outlook 2016 will not move emails in search results Using Outlook 4
K Using Outlook 2016 to draw Using Outlook 1
M Outlook 2016 Free/Busy settings ignored Using Outlook 0
K Outlook 2016 - controlling IMAP OST size with Group Policy not working Using Outlook 1
C Outlook 2016/2019 hangs after being open for an extended period Using Outlook 4
GregS 2016 Contact List being managed by Outlook.com? Using Outlook 1
A Outlook 2016 in Cached Mode Using Outlook 1
G Outlook 2016 sync contacts directly between phone and computer using outlook 2016 Using Outlook 0
N Outlook 2016 Folder Icons Look Using Outlook 0
Marc2019 Outlook 2016 Font Problem Using Outlook 5
B Outlook 2016 Does not Shutdown Correctly Using Outlook 3

Similar threads

Back
Top