我找到了一个VBA宏,它从Outlook收集电子邮件并将它们放入Excel中的单元格中。 代码有效,但我想告诉Excel只能从特定的子文件夹中获取电子邮件。在我的收件箱文件夹中,我有一个名为Info的子文件夹。我希望能够从这个子文件夹中收到电子邮件。
这是我现在的代码:
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
如何确保它从子文件夹获取电子邮件,而不是主文件夹?
答案 0 :(得分:2)
尝试使用以下代码:
'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"
' subfolder name
Dim subFolderName As String
subFolderName = "Info"
Set folder = Outlook.Session.folders(MailboxName).folders(Pst_Folder_Name).Folders(subFoldersName)
If folder = "" Then
MsgBox "Invalid Data in Input"
GoTo end_lbl1:
End If
此外,您可以考虑使用Namespace类的GetDefaultFolder方法获取一个Folder对象,该对象表示当前配置文件所请求类型的默认文件夹。