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