I've found a VBA macro that takes e-mails from Outlook and put them into cells in Excel. The code works, but I want to tell Excel only to get e-mails from a specific subfolder. In my Inbox-folder I have a subfolder called Info. I want to be able to get e-mails from this subfolder.
This is the code that I have now:
Sub Download_Outlook_Mail_To_Excel()
'Add Tools->References->"Microsoft Outlook nn.n Object Library"
'nn.n varies as per our Outlook Installation
Dim folders As Outlook.folders
Dim folder As Outlook.MAPIFolder
Dim iRow As Integer
Dim Pst_Folder_Name
Dim MailboxName
'Mailbox or PST Main Folder Name (As how it is displayed in your Outlook Session)
MailboxName = "My email address"
'Mailbox Folder or PST Folder Name (As how it is displayed in your Outlook Session)
Pst_Folder_Name = "Inbox"
Set folder = Outlook.Session.folders(MailboxName).folders(Pst_Folder_Name)
If folder = "" Then
MsgBox "Invalid Data in Input"
GoTo end_lbl1:
End If
'Rad Through each Mail and export the details to Excel for Email Archival
Sheets(1).Activate
For iRow = 1 To folder.Items.Count
Sheets(1).Cells(iRow, 1).Select
Sheets(1).Cells(iRow, 1) = folder.Items.Item(iRow).SenderName
Sheets(1).Cells(iRow, 2) = folder.Items.Item(iRow).Subject
Sheets(1).Cells(iRow, 3) = folder.Items.Item(iRow).ReceivedTime
Sheets(1).Cells(iRow, 4) = folder.Items.Item(iRow).Size
Sheets(1).Cells(iRow, 5) = folder.Items.Item(iRow).SenderEmailAddress
'Sheets(1).Cells(iRow, 6) = Folder.Items.Item(iRow).Body
Next iRow
MsgBox "Outlook Mails Extracted to Excel"
end_lbl1:
End Sub
How can I make sure that it gets emails from the subfolder, and not the mainfolder?
Try to use the following code:
Also you may consider using the GetDefaultFolder method of the Namespace class to get a Folder object that represents the default folder of the requested type for the current profile.