1

We are using a VBA macro in Excel to count mails in several Outlook subfolders. I'd also like to use it to count mails in searchfolders, but it isn't working.

The code loops through different Outlook folders, the location of each of this folders is available in a column in an Excel sheet. (mailbox@mail.com\folder\subfolder - with different possibilities of mailboxes / folders).

We refer to this folder with the following code:

set mailfolder = GetFolder(email_folder)

This is the GetFolder function:

Function GetFolder(ByVal strFolderPath As String) As MAPIFolder
    '    strFolderPath needs to be something like
    '    "Public Folders\All Public Folders\Company\Sales" or
    '    "Personal Folders\Inbox\My Folder"
    
    Dim objApp As Outlook.Application
    Dim objNS As Outlook.Namespace
    Dim colFolders As Outlook.Folders
    Dim objFolder As Outlook.MAPIFolder
    Dim arrFolders() As String
    Dim i As Long
        
    On Error Resume Next
        
    'strFolderPath = Replace(strFolderPath, "/", "\")
    arrFolders() = Split(strFolderPath, "\")
    Set objApp = CreateObject("Outlook.Application")
    Set objNS = objApp.GetNamespace("MAPI")
    Set objFolder = objNS.Folders.Item(arrFolders(0))
    If Not objFolder Is Nothing Then
        For i = 1 To UBound(arrFolders)
            Set colFolders = objFolder.Folders
            Set objFolder = Nothing
            Set objFolder = colFolders.Item(arrFolders(i))
            If objFolder Is Nothing Then
                Exit For
            End If
        Next
    End If
        
    Set GetFolder = objFolder
    Set colFolders = Nothing
    Set objNS = Nothing
    Set objApp = Nothing
        
    On Error GoTo 0
    
End Function

Is there a way to adapt this function to find the searchfolders?

Ceann
  • 13
  • 5
  • You may find the following useful as a way to get the information you need, although I don't know if it will work with search folders: https://stackoverflow.com/questions/64007790/how-can-one-iterate-through-the-subfolders-of-a-subfolder-of-a-shared-mail-inbox/64573722#64573722 – Tragamor Feb 17 '21 at 10:34
  • https://learn.microsoft.com/en-us/office/vba/api/outlook.store.getsearchfolders – Tragamor Feb 17 '21 at 10:37

1 Answers1

3

Use the folder name. No path.

Private Sub Test_Unread()
    FindSearchFolder "Unread Mail"
End Sub

Private Sub FindSearchFolder(fldrName As String)

    Debug.Print
    Debug.Print "Searching for " & fldrName
    
    Dim objStores As Stores
    Dim objStore As Store
        
    Dim objSearchFolders As folders
    Dim objSearchFolder As folder
    
    Dim objItem As Object
    
    Dim bFound As Boolean
    Dim i As Long
    
    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 ActiveExplorer.CurrentFolder = objSearchFolder
                Debug.Print objSearchFolder.Items.count
                
            End If
            
            If bFound = True Then Exit For
            
        Next
        
        If bFound = False Then Debug.Print " Not found in " & objStore
    Next
    
End Sub

If you find an error that is unfixable/unexplainable, when testing/manipulating searchfolders, close Outlook and restart.

niton
  • 8,771
  • 21
  • 32
  • 52
  • Thanks for your answer, it does work for my main email account, but not for other accounts (you don't have appropriate permission to perform this operation). I guess it is not allowed in our company to access this data.... – Ceann Feb 19 '21 at 08:28
  • 1
    See if you have a separate account for that email address https://stackoverflow.com/a/62731358/1571407. If not try to add an Account. https://support.microsoft.com/en-us/office/add-an-email-account-to-outlook-6e27792a-9267-4aa4-8bb6-c84ef146101b – niton Feb 19 '21 at 14:12