0
votes

I'm extracting subject lines of emails from Outlook folders. I reset a 'Folder' variable with each folder I need the subjects from.

Everything works except for the one instance where the folder is a search folder.

It looks like there is a SearchFolders thing I can Dim a variable as, but I don't know how to set a specific folder to it.

I can set the regular folders and sub folders with something like:

Set myFolder = myNamespace.Folders("[email protected]").Folders("Head folder").Folders("subfolder")

Do I need to Dim the 'SearchFolders'? How do I set a search folder as a variable?

2
Search folders are represented by the same MAPIFolder object as the regular folders. Are you getting a specific error? - Dmitry Streblechenko
Run-time error '-2147221233 (8004010f)': The attempted operation failed. An object could not be found. - Jeff T.
That error is MAPI_E_NOT_FOUND, which means a folder with the specified name does not exist. - Dmitry Streblechenko
My current reference is myNamespace.Folders("my email").Folders("Search Folders").Folders("Got it, will recover") . Even looking at it again, it doesn't look like anything is off in my spelling and this pattern works as long as the subfolder I'm reaching out to isn't under a search folder. Are you sure there is no difference between referencing search folders and non search ones?\ - Jeff T.
"Search Folders" in not listed in the MAPIFolder.Folders collection, it is just Outlook shows it there. - Dmitry Streblechenko

2 Answers

1
votes

To access search folders, use Store.GetSearchFolders() - it returns Folders collection with all search folders in the store (including search folders invisible to the end user).

1
votes
Sub FindSearchFolderByName()
    
    Dim objStores As Stores
    Dim objStore As Store
    
    Dim objSearchFolders As folders
    Dim objSearchFolder As Folder
    
    Dim fldrName As String
    Dim bFound As Boolean
    
    Dim myFolder As Folder
    
    fldrName = "Unread Mail"
    
    Debug.Print
    Debug.Print "Searching for " & fldrName
    
    Set objStores = Session.Stores
 
    For Each objStore In objStores
        Debug.Print
        Debug.Print "objStore: " & objStore
        bFound = False
        
        Set objSearchFolders = objStore.GetSearchFolders
        
        For Each objSearchFolder In objSearchFolders
        
            Debug.Print " objSearchFolder: " & objSearchFolder
            
            If objSearchFolder.Name = fldrName Then
            
                Debug.Print " Found in " & objStore
                bFound = True
                
                Set myFolder = objSearchFolder
                Set ActiveExplorer.CurrentFolder = myFolder
                
            End If
            
            If bFound = True Then Exit For
            
        Next
        
        If bFound = False Then Debug.Print " Not found in " & objStore
    Next
    
End Sub