The Outlook API wrongfully shows an outlook folder to have zero sub-folders. RRS feed

  • Question

  • Hello,

    I have a long love afair with outlook and recently I built
    a simple VSTO 2019 code to move mail items to outlook sub-folders.

    This is 365 non-default inbox folder (meaning not under the normal inbox, but rather
    the email are under a shared folder which more that one person can access).

    We use Outlook 365, version 1908 (build 11929.20708), connected with an Exchange account.

    The basic code it to iterate a folder path ("\\DRK\IncomingInbox\Customers\Cust01Box", "\\DRK\IncomingInbox\Customers\Cust02Box" etc),
    reach the final sub-folder and move the item to it.

    It works, however from time to time the code JAMS because
    outlook stops "seeing" the sub-folder under the "\\DRK\IncomingInbox\Customers\" parent folder.

    * By "not seeing" I mean that it reports that the parent "IncomingInbox" folder has ZERO sub folders.

    * When this happens, the line "Set oFolder = SubFolders.item(FoldersArray(i))" (code below) gives an exception with a err desc of 
    "The attempted operation failed.  An object could not be found.".

    At any given day this could flap multiple times (seeing and not seeing the sub-folders).

    The issue happens with VSTO (VS 2019) and also with VBA.
    * In VSTO, switched to  'g_oApp = Globals.ThisAddIn.Application' (instead of 'g_oApp = New Outlook.Application') ,
    however it did not help.

    Hope I made the issue clear and would love an advice about this matter.

    * Just to clarify, A sample of the code to reach a sub-folder
    (taken from slipstick.com):

    Function GetFolderPath(ByVal FolderPath As String) As Outlook.Folder
        Dim oFolder As Outlook.Folder
        Dim FoldersArray As Variant
        Dim i As Integer

        On Error GoTo GetFolderPath_Error
        If Left(FolderPath, 2) = "\\" Then
            FolderPath = Right(FolderPath, Len(FolderPath) - 2)
        End If
        'Convert folderpath to array
        FoldersArray = Split(FolderPath, "\")
        Set oFolder = Application.Session.Folders.item(FoldersArray(0))
        If Not oFolder Is Nothing Then
            For i = 1 To UBound(FoldersArray, 1)
                Dim SubFolders As Outlook.Folders
                Set SubFolders = oFolder.Folders
                Set oFolder = SubFolders.item(FoldersArray(i))
                If oFolder Is Nothing Then
                    Set GetFolderPath = Nothing
                End If
        End If
        'Return the oFolder
        Set GetFolderPath = oFolder
        Exit Function

        Set GetFolderPath = Nothing
        Exit Function
    End Function


    Monday, September 21, 2020 9:08 AM

All replies

  • Is it a shared store? Where does the store come from?

    Anyway, you may try to expand the folder programmatically. It can slow down your VBA macro or add-in, but can help with the issue you faced with. The Explorer.CurrentFolder property allows setting a Folder object that represents the current folder displayed in the explorer.

    Sub ChangeCurrentFolder() 
     Dim myNamespace As Outlook.NameSpace
     Set myNamespace = Application.GetNamespace("MAPI")
     Set Application.ActiveExplorer.CurrentFolder = myNamespace.GetDefaultFolder(olFolderCalendar) 
    End Sub

    And then you may try to access subfolders. Hopefully, the collection will not be empty.

    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers

    Monday, September 21, 2020 5:20 PM
  • Hi Eugene,

    Thanks for the reply.

    I wrote "shared" however meant this is a normal exchange folder (similar to my inbox)

    Originally, the basic code I wrote used these lines to access the base folder:
      Set objNS = GetNamespace("MAPI")
      Set objFolder = objNS.Folders("DRK")

    The speed up testing, I have added your suggestion to the following VBA
    to find out if there is a difference:

        If Not oFolder Is Nothing Then
            For i = 1 To UBound(FoldersArray, 1)
                Dim SubFolders As Outlook.Folders
                Set oSavePrevFolder = oFolder
                Set SubFolders = oFolder.Folders
                Debug.Print FoldersArray(i)
                On Error Resume Next
                Set oFolder = SubFolders.Item(FoldersArray(i))
                If Err.Number <> 0 Then
                    Debug.Print "folder error: " & Err.Description & vbCrLf & "Trying fix..."
                    Set Application.ActiveExplorer.CurrentFolder = oSavePrevFolder
                    On Error GoTo 0
                    Set oFolder = Application.ActiveExplorer.CurrentFolder.Folders.Item(FoldersArray(i))
                End If
                On Error GoTo 0
                If oFolder Is Nothing Then
                    Set GetFolderPath = Nothing
                End If
        End If

    One last thing-

    The Application.ActiveExplorer.CurrentFolder changes what the user sees on screen,
    which is something I would prefer to avoid, however what you suggested might help to better understand the issue.

    Tuesday, September 22, 2020 7:05 AM
  • Hi,

    Unfortunately, the issue happens also when I use "CurrenFolder"- please see below a debug :

    *It says zero sub-folders on "//DRK/Inbox"



    Tuesday, September 22, 2020 10:35 AM
  • Hello,

    Seems the issue has to do with the EntryID of the folder-

    When the issue happens, the EntryId of the folder is the WRONG .

    It can be seen by the following experiment-

    * I request the folder using the CORRECT entryid (and storeid)

      Set ns = Application.GetNamespace("MAPI")
      Set oFolder = ns.GetFolderFromID("000000001558C00B64527A4E83C186E33FCE9CCF018018D07E87680CDD48B628CC1483CA3B1B00001BAEBE590100",  _

    However, what I get is a folder with a DIFFERENT entryid


    The difference are small, but meaningfull:

    * at the end- 90100 (good one) VS 90000 (bad one).

    * in the middle- F018018 vs F010018 .

    Any IDs ?


    Saturday, October 3, 2020 1:50 PM