imlearningexcelvba
New Member
- Joined
- Mar 22, 2020
- Messages
- 9
- Office Version
- 365
- Platform
- Windows
I would like to extract out subject from shared outlook email. I manage to extract out inbox but subfolder are not extracted out. Anything wrong with my code below?
Shared email > Inbox > subfolder1 (under subfolder 1 has few folders)
Shared email > Inbox > subfolder1 (under subfolder 1 has few folders)
VBA Code:
Public xlSht As Excel.Worksheet
Sub DocumentFolders(objParent As Folder, lRow As Long)
Dim objItm As Object
Dim objFolder As Folder
Dim strMailboxName As String
Dim Ns As Outlook.Namespace
Dim olShareName As Outlook.Recipient
Dim subFolder As Object
Set OutlookApp = New Outlook.Application
Set Ns = OutlookApp.GetNamespace("MAPI")
Set olShareName = Ns.CreateRecipient("xxx@xxx.COM") '// Owner's email address
Set objParent = Ns.GetSharedDefaultFolder(olShareName, olFolderInbox) '// Inbox
On Error Resume Next
With xlSht
For Each objItm In objParent.Items
.Cells(lRow, 1) = objParent
.Cells(lRow, 2) = objItm.ReceivedTime
.Cells(lRow, 3) = objItm.Subject
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
Set xlApp = New Excel.Application
Set xlWb = xlApp.Workbooks.Add
Set xlSht = xlWb.Sheets(1)
With xlSht
.Cells(1, 1) = "Folder"
.Cells(1, 2) = "Received Time"
.Cells(1, 3) = "Subject"
End With
Call DocumentFolders(Session.GetDefaultFolder(olFolderInbox), 2)
xlApp.Visible = True
Set xlSht = Nothing
Set xlWb = Nothing
Set xlApp = Nothing
End Sub