2010-02-20 64 views

回答

2

我會說Office Automation是去這裏的方法.. 如果您安裝了Excel,您可以直接將屬性插入到工作表上的單元格中。您可以在Excel中編寫宏來自動化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

+0

馬庫斯,非常感謝你提供這個腳本!它在第一次嘗試中完美運行。我需要做的唯一改變就是從田野中取出逗號,因爲我的科目中有逗號。我也查找了MailItem對象,所以我可以訪問其他字段。非常感謝你,你爲我省下了很多時間! – Noel 2010-02-20 19:53:38

+0

順便說一句,我真的很喜歡能夠選擇文件夾,而不是硬編碼到腳本中的功能,好:) – Noel 2010-02-20 19:58:58

相關問題