Results 1 to 10 of 10

Thread: Search Outlook folders
Thanks Thanks: 0 Likes Likes: 0

  1. #1
    Board Regular
    Join Date
    Jun 2010
    Posts
    396
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Search Outlook folders

    Hi,

    found this code below how can it be modified to search for all folders/sub folders

    Code:
    Public Sub OutlookSearch()
    
    Dim strFilter As String
    Dim oMail As Outlook.MailItem
    Dim strDASLFilter As String
    Dim strScope As String
    
    
    SendKeys "{HOME}", True
    On Error GoTo Err_SearchFolderForSender
     
    strFilter = InputBox("Enter Search String:", "Search")
    If strFilter = "" Then
        Exit Sub
    End If
     
    ' lets get the email address from a selected message
    Set oMail = ActiveExplorer.Selection.Item(1)
    If strFilter = "" Then Exit Sub
     
    ' From email address
    Const From1 As String = "http://schemas.microsoft.com/mapi/proptag/0x0065001f"
    Const From2 As String = "http://schemas.microsoft.com/mapi/proptag/0x0042001f"
     
    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 & "%' "
      
    strScope = "Inbox"
     
    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
     
    Exit Sub
     
    Err_SearchFolderForSender:
    MsgBox "Error # " & Err & " : " & Error(Err)
     
    End Sub
    thanks

  2. #2
    Board Regular
    Join Date
    Oct 2007
    Posts
    5,740
    Post Thanks / Like
    Mentioned
    8 Post(s)
    Tagged
    2 Thread(s)

    Default Re: Search Outlook folders

    Replace the strScope = "Inbox" line with these lines:
    Code:
        Dim accountFolder As Folder, topFolder As Folder
        Dim allTopFolders As String
        
        allTopFolders = ""
        For Each accountFolder In Session.Folders
            For Each topFolder In accountFolder.Folders
                allTopFolders = allTopFolders & "'" & topFolder.Name & "',"
            Next
        Next
        strScope = Left(allTopFolders, Len(allTopFolders) - 1)
    strScope is then a list of all the top-level folders in every account.

  3. #3
    Board Regular
    Join Date
    Jun 2010
    Posts
    396
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Search Outlook folders

    thank you for the reply.

    I did but when click search and type the word looking for i get the following error:

    Error # 2147352567: Array index out of bounds

    Here is the complete code with your suggestion:

    Code:
    Public Sub NavigataAPSearch()Dim strFilter As String
    Dim oMail As Outlook.MailItem
    Dim strDASLFilter As String
    Dim strScope As String
    Dim accountFolder As Folder, topFolder As Folder
    Dim allTopFolders As String
    
    
    SendKeys "{HOME}", True
    On Error GoTo Err_SearchFolderForSender
     
    strFilter = InputBox("Enter Search String:", "Search")
    If strFilter = "" Then
        Exit Sub
    End If
     
    ' lets get the email address from a selected message
    Set oMail = ActiveExplorer.Selection.Item(1)
    If strFilter = "" Then Exit Sub
     
    ' From email address
    Const From1 As String = "http://schemas.microsoft.com/mapi/proptag/0x0065001f"
    Const From2 As String = "http://schemas.microsoft.com/mapi/proptag/0x0042001f"
     
    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 & "%' "
      
    'strScope = "Navigata AP"
    
    
     allTopFolders = ""
        For Each accountFolder In Session.Folders
            For Each topFolder In accountFolder.Folders
                allTopFolders = allTopFolders & "'" & topFolder.Name & "',"
            Next
        Next
        strScope = Left(allTopFolders, Len(allTopFolders) - 1)
     
    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
     
    Exit Sub
     
    Err_SearchFolderForSender:
    MsgBox "Error # " & Err & " : " & Error(Err)
     
    End Sub

  4. #4
    Board Regular
    Join Date
    Oct 2007
    Posts
    5,740
    Post Thanks / Like
    Mentioned
    8 Post(s)
    Tagged
    2 Thread(s)

    Default Re: Search Outlook folders

    Comment out the On Error GoTo line and rerun the macro. Click the Debug button when the error message appears - which line is highlighted in yellow?

  5. #5
    Board Regular
    Join Date
    Jun 2010
    Posts
    396
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Search Outlook folders

    tried what happened it give a different error pop up message will not let run debug unless i click OK the error.

    now giving me different error # 2147024809: Sorry, something went wrong. You may want to try again.

  6. #6
    Board Regular
    Join Date
    Oct 2007
    Posts
    5,740
    Post Thanks / Like
    Mentioned
    8 Post(s)
    Tagged
    2 Thread(s)

    Default Re: Search Outlook folders

    Do you have multiple accounts in your Outlook? If so, then the code I suggested is wrong because it creates the strScope string containing the top-level folders for all the accounts and you'll get that 'Sorry, something went wrong' error, because AdvancedSearch is trying to search all the accounts at once.

    The solution is to run AdvancedSearch separately on each account, creating strScope as the list of top-level folders for each account:

    Code:
    Public Sub Outlook_AdvancedSearch_All_Accounts()
    
        Dim strFilter As String
        Dim strDASLFilter As String
        Dim strScope As String
        Dim accountFolder As Folder, topFolder As Folder
        Dim allTopFolders As String
        Dim objSearch As Search
        
        'On Error GoTo Err_SearchFolderForSender
         
        strFilter = InputBox("Enter Search String:", "Search")
        If strFilter = "" Then Exit Sub
              
        ' From email address
        Const From1 As String = "http://schemas.microsoft.com/mapi/proptag/0x0065001f"
        Const From2 As String = "http://schemas.microsoft.com/mapi/proptag/0x0042001f"
         
        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 & "%' "
          
        'Run Advanced Search on all top-level folders in each account
        
        For Each accountFolder In Session.Folders
        
            'Create list of top-level folders in this account
            
            allTopFolders = ""
            For Each topFolder In accountFolder.Folders
                allTopFolders = allTopFolders & "'" & topFolder.Name & "',"
            Next
            strScope = Left(allTopFolders, Len(allTopFolders) - 1)
            
            Set objSearch = Application.AdvancedSearch(Scope:=strScope, Filter:=strDASLFilter, SearchSubFolders:=True, Tag:="SearchFolder")
            
            'Save the search results in a folder inside Search Folders in the Outlook UI
            
            objSearch.Save strFilter
            
            Set objSearch = Nothing
        
        Next
    
        Exit Sub
         
    Err_SearchFolderForSender:
        MsgBox "Error # " & Err & " : " & Error(Err)
     
    End Sub
    If you still get an error on the AdvancedSearch for multiple accounts with the above macro then try this macro which searches only a single specific account.

    The account to search is this line:

    Code:
        Set accountFolder = Session.Folders(1)  'or Session.Folders("Account Name")
    where you can specify the account by index number or name.

    Code:
    Public Sub Outlook_AdvancedSearch_Specific_Account()
    
        Dim strFilter As String
        Dim strDASLFilter As String
        Dim strScope As String
        Dim accountFolder As Folder, topFolder As Folder
        Dim allTopFolders As String
        Dim objSearch As Search
        
        'On Error GoTo Err_SearchFolderForSender
         
        strFilter = InputBox("Enter Search String:", "Search")
        If strFilter = "" Then Exit Sub
              
        ' From email address
        Const From1 As String = "http://schemas.microsoft.com/mapi/proptag/0x0065001f"
        Const From2 As String = "http://schemas.microsoft.com/mapi/proptag/0x0042001f"
         
        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 & "%' "
          
        'Run Advanced Search on all top-level folders for a single specific account
        
        Set accountFolder = Session.Folders(1)  'or Session.Folders("Account Name")
        
        'Create list of top-level folders in this account
        
        allTopFolders = ""
        For Each topFolder In accountFolder.Folders
            allTopFolders = allTopFolders & "'" & topFolder.Name & "',"
        Next
        strScope = Left(allTopFolders, Len(allTopFolders) - 1)
        
        Set objSearch = Application.AdvancedSearch(Scope:=strScope, Filter:=strDASLFilter, SearchSubFolders:=True, Tag:="SearchFolder")
        
        'Save the search results in a folder inside Search Folders in the Outlook UI
        
        objSearch.Save strFilter
        
        Set objSearch = Nothing
    
        Exit Sub
         
    Err_SearchFolderForSender:
        MsgBox "Error # " & Err & " : " & Error(Err)
     
    End Sub
    Last edited by John_w; Jun 15th, 2019 at 09:38 AM.

  7. #7
    Board Regular
    Join Date
    Jun 2010
    Posts
    396
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Search Outlook folders

    Yes, there multiple accounts.

    I was away for a month when came back they created a share account called ap that's why also my rules not working either.

    I'm using Outlook 2016 is office 365 plus package.

    Thank you for all your help, much appreciated.

  8. #8
    Board Regular
    Join Date
    Jun 2010
    Posts
    396
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Search Outlook folders

    Hi,

    I know this question is stupid, how do i know what's the account name or index number in outlook? for example Inbox is that the account name, and the share folder is called NCL AP is tat the other account name?

    sorry I am not that familiar with account name or index in outlook.

    thank you,

  9. #9
    Board Regular
    Join Date
    Oct 2007
    Posts
    5,740
    Post Thanks / Like
    Mentioned
    8 Post(s)
    Tagged
    2 Thread(s)

    Default Re: Search Outlook folders

    The account name is the 'folder' you see above the Inbox and contains all the top-level folders, including the Inbox for that account.

    Just try different index numbers starting at 1 and add this MsgBox line to display the name of that account and run the macro again until it shows the correct account:

    Code:
    Set accountFolder = Session.Folders(1)   'Try 1, 2 
    MsgBox "Account Name = " & accountFolder.Name
    Stop
    "NCL AP" might be the account name - I'm not sure how accounts work with shared folders. You could try:
    Code:
    Set accountFolder = Session.Folders("NCL AP")
    Last edited by John_w; Jun 16th, 2019 at 04:55 PM.

  10. #10
    Board Regular
    Join Date
    Jun 2010
    Posts
    396
    Post Thanks / Like
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)

    Default Re: Search Outlook folders

    thank you very much for your help.

    with your tips i was able to found out the second account name which is index 1

    but run code gave me run time error 2147024809 (80070057) and highlight this line of the code:

    Code:
    Set objSearch = Application.AdvancedSearch(Scope:=strScope, Filter:=strDASLFilter, SearchSubFolders:=True, Tag:="SearchFolder")
    thanks again

Some videos you may like

User Tag List

Tags for this Thread

Like this thread? Share it with others

Like this thread? Share it with others

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •