I'm not super familiar with the Outlook object model, and it has been a while since I've VBA'd - I appreciate any help. I am adapting someone else's Excel VBA to scrape a shared Outlook mailbox (I need the results in Excel, so that works for me).
The code currently works on the main inbox folder (it pulls all the emails in the inbox), but I'd like to scrape all subfolders as well (there may be multiple levels of subfolders in the shared Outlook inbox).
Fiddling with the code and other online examples, I can get all the top-level list of mailboxes (mine, and those shared with me). I can also get the top level items within the shared mailbox (inbox, sent, drafts, archive). I just can't figure out the syntax to get the subfolders of the shared inbox.
Windows, local install of office. Everything is x64.
Thank you for any assistance/advice!
Sub GetFromOutlook()
Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim mFolder As MAPIFolder
Dim olParentFolder As MAPIFolder
Dim sFolder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer
Dim olNS As Outlook.Namespace
Dim subfolder As Folder
Dim objItm As Object
Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
'**** this bit is added to get a shared email ******
Set objOwner = OutlookNamespace.CreateRecipient("name@company.com")
objOwner.Resolve
If objOwner.Resolved Then
Set mFolder = OutlookNamespace.GetSharedDefaultFolder(objOwner, olFolderInbox) 'can use olFolderInbox or olFolderSentMail
End If
'failed attempts to try to find a way to reference the subfolders
Debug.Print mFolder.Name
'Debug.Print mFolder.objItm.Count
'Set subfolder = mFolder.Folders(1)
'Debug.Print subfolder.Name
For Each myObj In OutlookNamespace.Folders
Debug.Print myObj.Name
Next
' Failed attempt
Dim olFolderA As Outlook.MAPIFolder
Dim olFolderB As Outlook.MAPIFolder
For Each olFolderA In mFolder.Folders
Debug.Print olFolderA.FolderPath, olFolderA.Items.Count, olFolderA.Folders.Count
For Each olFolderB In olFolderA.Folders
Debug.Print olFolderB.FolderPath, olFolderB.Items.Count
Next
Next
'more fail
Dim objItems As Variant
Set objItems = mFolder.Parent.Folders
'Dim f As Outlook.MAPIFolder
For Each f In mFolder.Parent.Folders
'DrillDown f
Debug.Print f.FolderPath, f.Items.Count
Next
' For Each objItm In mFolder.Items
' Debug.Print objItm.Parent
' Next
'***************************************************
'Write mail to spreadsheet - create named range, from and to dates on the spreadsheet
i = 1
'On Error Resume Next
'For Each Folder In Namespace.Folders
' For Each subfolder In Folder.Folders
Debug.Print Folder.Name '& " | " & subfolder.Name 'still fail
For Each OutlookMail In Folder.Items
Sheet2.Range("A" & i).Value = i
Sheet2.Range("B" & i).Value = OutlookMail.ReceivedTime
Sheet2.Range("C" & i).Value = OutlookMail.SenderName
Sheet2.Range("D" & i).Value = OutlookMail.To
Sheet2.Range("E" & i).Value = OutlookMail.Subject
Sheet2.Range("F" & i).Value = OutlookMail.Body
OriginalBodyFull = OutlookMail.Body
i = i + 1
'End If
Next OutlookMail
'Next subfolder
'Next Folder
'On Error GoTo 0
Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing
End Sub
I found one thread where the shared mailbox settings seemed to be part of the problem/solution, so adding that here as well