简体   繁体   中英

Extract from Shared Mailbox Outlook Subfolder is not working

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

"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")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM