将部分邮件正文从Outlook提取到Excel

时间:2016-02-24 05:13:50

标签: excel vba excel-vba email outlook

您好我正在尝试提取电子邮件正文的特定部分以及我拥有相同部分的电子邮件数量。我使用下面的vba代码,但我遇到了以下问题:

  1. 输出没有填充,但脚本正在运行而不会失败。
  2. 无法从电子邮件正文中提取该特定部分。
  3. 使用的代码是:

    Option Explicit
    Sub Download_Outlook_Mail_To_Excel()
    Dim Folder          As Outlook.MAPIFolder
    Dim sFolders        As Outlook.MAPIFolder
    Dim iRow            As Integer
    Dim oRow            As Integer
    Dim MailBoxName     As String
    Dim Pst_Folder_Name As String
    
    Const xlWorkbookName As String = "C:\Personal\Documents\Failures.xlsx" '// change as required
    
    '// I'm using late binding in case you don't actually have a reference set.
    Dim xlApp           As Object
    Dim xlWB            As Object
    
    Set xlApp = CreateObject("Excel.Application")
    xlApp.Visible = False
    
    Set xlWB = xlApp.Workbooks.Open(xlWorkbookName)
    
    MailBoxName = "ridutta@gmail.com"
    
    
    Pst_Folder_Name = "SR Creation Failure" 'Sample "Inbox" or "Sent Items"
    
    'To directly a Folder at a high level
    'Set Folder =  Outlook.Session.Folders(MailBoxName).Folders(Pst_Folder_Name)
    
    'To access a main folder or a subfolder (level-1)
    For Each Folder In Outlook.Session.Folders(MailBoxName).Folders
        If VBA.UCase(Folder.Name) = VBA.UCase(Pst_Folder_Name) Then GoTo Label_Folder_Found
        For Each sFolders In Folder.Folders
            If VBA.UCase(sFolders.Name) = VBA.UCase(Pst_Folder_Name) Then
                Set Folder = sFolders
                GoTo Label_Folder_Found
            End If
        Next sFolders
    Next Folder
    
    Label_Folder_Found:
    If Folder.Name = "" Then
        MsgBox "Invalid Data in Input"
        GoTo End_Lbl1:
    End If
    
    'Read Through each Mail and export the details to Excel for Email Archival
    xlWB.Sheets(1).Activate
    Folder.Items.Sort "Received"
    
    'Insert Column Headers
    xlWB.Sheets(1).Cells(1, 1) = "Sender"
    xlWB.Sheets(1).Cells(1, 2) = "Subject"
    xlWB.Sheets(1).Cells(1, 3) = "Date"
    xlWB.Sheets(1).Cells(1, 4) = "Size"
    xlWB.Sheets(1).Cells(1, 5) = "EmailID"
    'ThisWorkbook.Sheets(1).Cells(1, 6) = "Body"
    
    'Export eMail Data from PST Folder
    oRow = 1
    For iRow = 1 To Folder.Items.Count
        'If condition to import mails received in last 60 days
        'To import all emails, comment or remove this IF condition
       If VBA.DateValue(VBA.Now) -  VBA.DateValue(Folder.Items.Item(iRow).ReceivedTime) <= 60 Then
           oRow = oRow + 1
           xlWB.Sheets(1).Cells(oRow, 1).Select
           xlWB.Sheets(1).Cells(oRow, 1) = Folder.Items.Item(iRow).SenderName
           xlWB.Sheets(1).Cells(oRow, 2) = Folder.Items.Item(iRow).Subject
           xlWB.Sheets(1).Cells(oRow, 3) = Folder.Items.Item(iRow).ReceivedTime
           xlWB.Sheets(1).Cells(oRow, 4) = Folder.Items.Item(iRow).Size
           xlWB.Sheets(1).Cells(oRow, 5) = Folder.Items.Item(iRow).SenderEmailAddress
           'ThisWorkbook.Sheets(1).Cells(oRow, 6) = Folder.Items.Item(iRow).Body
        End If
    Next iRow
    MsgBox "Outlook Mails Extracted to Excel"
    Set Folder = Nothing
    Set sFolders = Nothing
    
    xlWB.Close False
    Set xlWB = Nothing
    
    xlApp.Quit
    Set xlApp = Nothing
    
    End_Lbl1:
    End Sub
    

1 个答案:

答案 0 :(得分:0)

使用正则表达式提取您要查找的电子邮件正文部分。请参阅:How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops