简体   繁体   中英

Extract partail email body from Outlook to Excel

Hello I am trying to extract a specific part of Email body and a count of how many emails with that same part I have got. I am using the below vba code but am getting the following issues:

  1. Output is not populating however the script is running without fail.
  2. unable to extract that specific part from the email body.

Code am using is:

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

Use a regular expression to extract the part of the email body you are looking for. Refer to this: How to use Regular Expressions (Regex) in Microsoft Excel both in-cell and loops

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM