[英]Exporting emails from an Outlook subfolder to Excel
I've found a VBA macro that takes e-mails from Outlook and put them into cells in Excel. 我发现了一个VBA宏,它可以从Outlook中接收电子邮件并将其放入Excel的单元格中。 The code works, but I want to tell Excel only to get e-mails from a specific subfolder.
该代码有效,但我只想告诉Excel从特定子文件夹中获取电子邮件。 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: 尝试使用以下代码:
'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
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. 您也可以考虑使用Namespace类的GetDefaultFolder方法来获取Folder对象,该对象代表当前配置文件所请求类型的默认文件夹。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.