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

时间:2010-02-20 02:36:35

标签: csv outlook export outlook-2007 export-to-excel

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

1 个答案:

答案 0 :(得分:2)

我会说办公自动化是这里的方式.. 如果安装了Excel,则可以直接将属性插入工作表上的单元格中。你可以在Excel中编写一个宏来自动化outlook,或者你可以在outlook中编写一个宏来将数据推送到工作表中。

下面我创建了一个快速的VBA for outlook,并使用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  

马库斯