简体   繁体   English

将Outlook 2013搜索文件夹电子邮件导出到Excel

[英]Export Outlook 2013 Search Folders emails to Excel

I would like to export my emails in the Search Folders to Excel . 我想将搜索文件夹中的电子邮件导出到Excel

I am using the code below to export my email in the Inbox. 我使用下面的代码在收件箱中导出我的电子邮件。

But with Search Folders it gives me an error : 但是使用搜索文件夹它会给我一个错误:

Run-time error '-2147221233 (8004010f) 运行时错误'-2147221233(8004010f)

The attempted operation failed. 尝试的操作失败。 An object could not be found. 找不到对象。

Sub Download_Outlook_Mail_To_Excel()

Dim folders As Outlook.folders
Dim folder As Outlook.MAPIFolder
Dim iRow As Integer
Dim Pst_Folder_Name
Dim MailboxName

MailboxName = "xxx@yy.com"
Pst_Folder_Name = "Inbox"

Set folder = Outlook.Session.folders(MailboxName).folders(Pst_Folder_Name)

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).Categories
Next iRow

MsgBox "Outlook Mails Extracted to Excel"

End Sub

I finally found the solution. 我终于找到了解决方案。 It only works with one specified Search Folders, named in the variable: Pst_Folder_Name. 它仅适用于一个指定的搜索文件夹,在变量中命名:Pst_Folder_Name。 If you have more search folders you have to use a cycle somehow. 如果你有更多的搜索文件夹,你必须以某种方式使用一个循环。

Sub Outlook_Emails_Handled_Last_Week()
    Dim colStores As Outlook.Stores
    Dim oStore As Outlook.Store
    Dim oSearchFolders As Outlook.folders
    Dim oFolder As Outlook.folder
    Dim mail As Outlook.MailItem
    Dim iRow As Integer
    Dim Pst_Folder_Name
    Dim MailboxName
    Dim Ws As Excel.Worksheet
    Dim LastRow As Integer

    MailboxName = "xxxx@yyy.com"
    Pst_Folder_Name = "Emails Handled Last Week"

    Set oFolder = Session.Stores.Item(MailboxName).GetSearchFolders(Pst_Folder_Name)

    Set Ws = ThisWorkbook.Worksheets("Sheet1")
    Ws.Activate

    LastRow = Ws.Range("A" & Ws.Rows.Count).End(xlUp).Row

        Ws.Cells(1, 1).Value = "Sender Name"
        Ws.Cells(1, 2).Value = "Subject"
        Ws.Cells(1, 3).Value = "Received Time"
        Ws.Cells(1, 4).Value = "Categories"

    For iRow = 1 To oFolder.Items.Count
        Ws.Cells(LastRow + iRow, 1) = oFolder.Items.Item(iRow).SenderName
        Ws.Cells(LastRow + iRow, 2) = oFolder.Items.Item(iRow).Subject
        Ws.Cells(LastRow + iRow, 3) = oFolder.Items.Item(iRow).ReceivedTime
        Ws.Cells(LastRow + iRow, 4) = oFolder.Items.Item(iRow).Categories

    Next iRow

    MsgBox "Completed!"

    End Sub

As Search folders can be preassigned or custom, the best way to detect them is to check the folder's .Class , which should be olSearch . 由于搜索文件夹可以预先分配或自定义,检测它们的最佳方法是检查文件夹的.Class ,它应该是olSearch

So this should work (I added a new Integer to keep writing if there is more than one Search folder) : 所以这应该工作(如果有多个Search文件夹,我添加了一个新的Integer来继续写入):

Sub Download_Outlook_Mail_To_Excel()

Dim folder As Outlook.MAPIFolder
Dim iRow As Integer
Dim LastWrow As Integer
Dim MailboxName As String
Dim Ws As Excel.Worksheet

MailboxName = "xxyy@zzz.com"
Set Ws = sheets(1)

For Each folder In Outlook.Session.folders(MailboxName).folders
    With folder
        If .Class <> olSearch Then
        Else
            Ws.Activate
            LastWrow = Ws.Range("A" & Ws.rows.Count).End(xlup).Row
            For iRow = 1 To .Items.Count
                'ws.Cells(iRow, 1).Select
                Ws.Cells(LastWrow + iRow, 1) = .Items.Item(iRow).SenderName
                Ws.Cells(LastWrow + iRow, 2) = .Items.Item(iRow).Subject
                Ws.Cells(LastWrow + iRow, 3) = .Items.Item(iRow).ReceivedTime
                Ws.Cells(LastWrow + iRow, 4) = .Items.Item(iRow).Categories
            Next iRow
        End If
    End With
Next folder

MsgBox "Outlook Mails Extracted to Excel"

End Sub

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用 Outlook 2013/Excel 2013 发送邮件 - Sending Emails with Outlook 2013/Excel 2013 使用带有 Excel VBA 的 Exchange 服务器从 Outlook 2013 导入电子邮件 - Import emails from Outlook 2013 with exchange server with Excel VBA Outlook 在 Excel 工作簿中导出多封电子邮件,但不同的 excel 工作表 - Outlook export multiple emails in Excel workbook but different excel worksheets 根据 Excel 中的数据集使用 VBA 将 Outlook 文件夹中的电子邮件移动到子文件夹 - Moving Emails in Outlook Folders to Subfolder with VBA according to Datasets in Excel 将电子邮件从Outlook文件夹导出到Excel。 编码问题 - Exporting Emails from Outlook Folders into Excel. Coding issue 从公用文件夹导出 Outlook 电子邮件以放入 Excel 工作表 - Export Outlook Emails from a Public folder to put inside Excel sheet 从Excel(2013)的备用(不是主要)Outlook(2013)帐户发送电子邮件。 (VBA) - Sending emails from Excel (2013) from an alternate (not main) Outlook (2013) account. (VBA) vba 将电子邮件从 Outlook 导出到 excel 并自动关闭 excel 并保存更改 - vba export emails from outlook to excel and automatically close excel and save changes 如何根据标准使用 Excel VBA 从 Outlook 中的所有文件夹和子文件夹中计算电子邮件? - How to count emails, according to criterion, from all folders and subfolders in Outlook using Excel VBA? Excel VBA with Outlook:如何使用AdvancedSearch方法在Outlook中搜索特定电子邮件? - Excel VBA with Outlook: How to use AdvancedSearch method to search for specific emails in Outlook?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM