-1

I use this code below to extract the subjects from Inbox and Subfolders/sub-sub folders if any. Is working fine on my main Mailbox where it extracted INBOX and SubFolders.

I have few shared mailbox in outlook. When I try to call the shared mailbox, it only extracted the shared mailbox INBOX but not the subfolders.

Anything wrong with my codes? Or anything I shall add on?

Public xlSht As Excel.Worksheet

Sub DocumentFolders(objParent As Folder, lRow As Long)
Dim objItm As Object
Dim objFolder As Folder

    On Error Resume Next
    With xlSht
        For Each objItm In objParent.Items
            .Cells(lRow, 1) = objParent
            .Cells(lRow, 2) = objItm.Subject
            .Cells(lRow, 3) = objItm.ReceivedTime
            lRow = lRow + 1
        Next
    End With
    On Error GoTo 0

    If objParent.Folders.Count > 0 Then
        For Each objFolder In objParent.Folders
            Call DocumentFolders(objFolder, lRow)
        Next
    End If

End Sub


Sub ExportInformation()
Dim xlApp As Excel.Application
Dim xlWb As Excel.Workbook

  Dim Ns As Outlook.Namespace
  Dim olShareName As Outlook.Recipient

  Set outlookApp = New Outlook.Application
  Set Ns = outlookApp.GetNamespace("MAPI")

  Set olShareName = Ns.CreateRecipient("xxxxx@xxx.com") '// Owner's email address
  olShareName.Resolve
  Set objParent = Ns.GetSharedDefaultFolder(olShareName, olFolderInbox) '// Inbox

    Set xlApp = New Excel.Application
    Set xlWb = xlApp.Workbooks.Add
    Set xlSht = xlWb.Sheets(1)

    With xlSht
        .Cells(1, 1) = "Folder"
        .Cells(1, 2) = "Subject"
        .Cells(1, 3) = "Received Time"
    End With

     Call DocumentFolders(Session.GetSharedDefaultFolder(olShareName, olFolderInbox), 2)

    xlApp.Visible = True


Set xlSht = Nothing
Set xlWb = Nothing
Set xlApp = Nothing

End Sub
BigBen
  • 46,229
  • 7
  • 24
  • 40
user3959104
  • 65
  • 1
  • 9
  • Does this answer your question? [How to set Outlook sub-folders of a shared default folder in VBA?](https://stackoverflow.com/questions/56938788/how-to-set-outlook-sub-folders-of-a-shared-default-folder-in-vba) – niton Mar 31 '20 at 16:08
  • https://stackoverflow.com/a/60823960/4539709 – 0m3r Mar 31 '20 at 16:28
  • @niton I try this method 'Try to turn caching off for the delegate folders - uncheck the "Download shared folders" checkbox on the Advanced tab of the Exchange account properties dialog.' But it is taking forever, and hang after sometime. Any advice how can I fix this? – user3959104 Apr 01 '20 at 01:43
  • If you have confirmed the subfolders are now available, I suggest you could try multiple extracts perhaps referencing each subfolder one level under the inbox. – niton Apr 01 '20 at 02:35
  • @niton Any samples I able to refer as you suggest? What shall i add on my codes above? – user3959104 Apr 01 '20 at 02:45

1 Answers1

0

"I try this method 'Try to turn caching off for the delegate folders - uncheck the "Download shared folders" checkbox on the Advanced tab of the Exchange account properties dialog.' But it is taking forever, and hang after sometime." How to set Outlook sub-folders of a shared default folder in VBA?

Try releasing memory.

Sub DocumentFolders(objParent As Folder, lRow As Long, xlSht As Excel.Worksheet)

    ' ...

            lRow = lRow + 1

            ' apparently objItm is not replaced in memory by the next objItm
            ' releasing this memory in the loop may help keep Excel from hanging
            Set objItm = Nothing

        Next

    ' ...

If not sufficient then reduce the number of folders processed in a run.

Set objParent = Ns.GetSharedDefaultFolder(olShareName, olFolderInbox) '// Inbox
Set objParent = objParent.folders("name of any subfolder one level under inbox")
niton
  • 8,771
  • 21
  • 32
  • 52