将 Outlook 2007 邮件文件夹和子文件夹导出到 csv

发布于 2024-08-22 20:05:44 字数 100 浏览 1 评论 0原文

将邮件从 Outlook 2007 文件夹导出到 CSV 文件的最佳方法是什么?我也想在子文件夹中包含邮件消息。内置的 csv 导出器不允许包含子文件夹的选项,但在其他方面正是我想要的。

What's the best way to export mail from an Outlook 2007 folder to a CSV file? I would like to include mail messages within subfolders as well. The built in csv exporter does not allow the option to include subfolders but otherwise does exactly what i want.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

合久必婚 2024-08-29 20:05:44

我想说办公自动化是通往这里的出路..
如果您安装了 Excel,则可以直接将属性插入工作表上的单元格中。您可以在 Excel 中编写宏以自动化 Outlook,也可以在 Outlook 中编写宏以将数据推送到工作表中。

下面我为 Outlook 创建了一个快速的 VBA 片段,并使用 FSO 来完成这些肮脏的工作,它将为您提供一个工作框架,它将需要更多的错误处理测试等。

Sub SaveItemsToExcel()

    On Error GoTo ErrorHandlerExit


   Dim oNameSpace As Outlook.NameSpace
   Dim oFolder As Outlook.MAPIFolder
   'You must set a reference to the Microsoft Scripting Runtime library touse the FileSystemObject

   Dim objFS As Scripting.FileSystemObject
   Dim objOutputFile As Scripting.TextStream

   Set objFS = New Scripting.FileSystemObject
   Set objOutputFile = objFS.OpenTextFile("C:\Temp\Export.csv", ForWriting, True)
   Set oNameSpace = Application.GetNamespace("MAPI")
   Set oFolder = oNameSpace.PickFolder

   If oFolder Is Nothing Then
      GoTo ErrorHandlerExit
   End If


    ' Check if folder can contain Mail Items
    If oFolder.DefaultItemType <> olMailItem Then
      MsgBox "Folder does not contain mail messages"
      GoTo ErrorHandlerExit
    End If


   'Write header line
    objOutputFile.WriteLine "From,Subject,Recived"

    ProcessFolderItems oFolder, objOutputFile

    objOutputFile.Close

    Set oFolder = Nothing
    Set oNameSpace = Nothing
    Set objOutputFile = Nothing
    Set objFS = Nothing

ErrorHandlerExit:
   Exit Sub


End Sub

Sub ProcessFolderItems(oParentFolder As Outlook.MAPIFolder, ByRef objOutputFile As Scripting.TextStream)
    Dim oCount As Integer
    Dim oMail As Outlook.MailItem
    Dim oFolder As Outlook.MAPIFolder
    oCount = oParentFolder.Items.Count

    For Each oMail In oParentFolder.Items
        If oMail.Class = olMail Then

        objOutputFile.WriteLine oMail.SenderEmailAddress & "," & oMail.Subject & "," & oMail.ReceivedTime

        End If
    Next oMail

    Set oMail = Nothing
    'check to see if we have an child folders
    If (oParentFolder.Folders.Count > 0) Then
            For Each oFolder In oParentFolder.Folders
                ProcessFolderItems oFolder, objOutputFile
            Next
    End If


End Sub  

Marcus

I would say that Office Automation is the way to go here ..
If you have Excel installed you can directly insert the properties into the Cells on a worksheet. You could write a macro in Excel to automate outlook or you could write a macro in outlook to push the data into a worksheet.

Below I have created a quick piece of VBA for outlook and used FSO to do the dirty work instead, It will give you a skeleton to work from, it will need a lot more error handling testing etc.

Sub SaveItemsToExcel()

    On Error GoTo ErrorHandlerExit


   Dim oNameSpace As Outlook.NameSpace
   Dim oFolder As Outlook.MAPIFolder
   'You must set a reference to the Microsoft Scripting Runtime library touse the FileSystemObject

   Dim objFS As Scripting.FileSystemObject
   Dim objOutputFile As Scripting.TextStream

   Set objFS = New Scripting.FileSystemObject
   Set objOutputFile = objFS.OpenTextFile("C:\Temp\Export.csv", ForWriting, True)
   Set oNameSpace = Application.GetNamespace("MAPI")
   Set oFolder = oNameSpace.PickFolder

   If oFolder Is Nothing Then
      GoTo ErrorHandlerExit
   End If


    ' Check if folder can contain Mail Items
    If oFolder.DefaultItemType <> olMailItem Then
      MsgBox "Folder does not contain mail messages"
      GoTo ErrorHandlerExit
    End If


   'Write header line
    objOutputFile.WriteLine "From,Subject,Recived"

    ProcessFolderItems oFolder, objOutputFile

    objOutputFile.Close

    Set oFolder = Nothing
    Set oNameSpace = Nothing
    Set objOutputFile = Nothing
    Set objFS = Nothing

ErrorHandlerExit:
   Exit Sub


End Sub

Sub ProcessFolderItems(oParentFolder As Outlook.MAPIFolder, ByRef objOutputFile As Scripting.TextStream)
    Dim oCount As Integer
    Dim oMail As Outlook.MailItem
    Dim oFolder As Outlook.MAPIFolder
    oCount = oParentFolder.Items.Count

    For Each oMail In oParentFolder.Items
        If oMail.Class = olMail Then

        objOutputFile.WriteLine oMail.SenderEmailAddress & "," & oMail.Subject & "," & oMail.ReceivedTime

        End If
    Next oMail

    Set oMail = Nothing
    'check to see if we have an child folders
    If (oParentFolder.Folders.Count > 0) Then
            For Each oFolder In oParentFolder.Folders
                ProcessFolderItems oFolder, objOutputFile
            Next
    End If


End Sub  

Marcus

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文