簡體   English   中英

從Outlook提取Partail電子郵件正文到Excel

[英]Extract partail email body from Outlook to Excel

您好,我正在嘗試提取電子郵件正文的特定部分以及我收到的具有相同部分的電子郵件數量。 我正在使用以下vba代碼,但遇到以下問題:

  1. 輸出沒有填充,但是腳本正在正常運行。
  2. 無法從電子郵件正文中提取該特定部分。

代碼正在使用的是:

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

使用正則表達式提取您要查找的電子郵件正文部分。 引用此: 如何在單元內和循環中在Microsoft Excel中使用正則表達式(Regex)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM