Obtain “sender” and “emailbody” properties

荒凉一梦 提交于 2020-01-06 08:21:08

问题


Background
I scan the inbox in Outlook and report the results to a Excel spreadsheet based on the Title of the email. I will use the same example as in Microsoft office keyword and will say "Office".

IE: Office: Problem with Laptop. I need to get the user name or email address that sent the mail and probably some keywords in the body of the email itself.
I found the way to iterate through the items that have this keyword only by using tables and rows.


Problem
I have not been able to find a way to cast the row.item from the table to an email nor to obtain "sender" or "emailbody" properties.


Code
You need to add Outlook reference

Option Base 1
Sub Outlook_ScanForEmails()
Const TxtTag  As String = "http://schemas.microsoft.com/mapi/proptag/"
Const TxtWordSubject As String = "Office:"
Dim OutTable As Outlook.Table
Dim OutRow As Outlook.Row
Dim OutEmail As Outlook.MailItem
Dim OutApp As Outlook.Application: Set OutApp = New Outlook.Application
Dim CounterEmails As Long
Dim TotalEmails As Long
Dim TxtFilter As String: TxtFilter = "@SQL=" & Chr(34) & TxtTag & "0x0037001E" & Chr(34) & " ci_phrasematch '" & TxtWordSubject & "'"
Dim TxtCourse As String
Dim DteReport As Date
Set OutTable = OutApp.Session.GetDefaultFolder(olFolderInbox).GetTable(TxtFilter)
    TotalEmails = OutTable.GetRowCount
    For CounterEmails = 1 To TotalEmails
    Set OutRow = OutTable.GetNextRow
    DteReport = OutRow("LastModificationTime")
    TxtCourse = OutRow("Subject")
    TxtCourse = Right(TxtCourse, Len(TxtCourse) - Len(TxtWordSubject))
    Next CounterEmails

End Sub


Further thoughts
I would prefer to not iterate through each email since the table narrows the process to iterating only the row items I need.


回答1:


Per my comment you can get a mail item from the entryID column of the table. Here is an example of how to accomplish this.

Option Base 1
Sub Outlook_ScanForEmails()
Const TxtTag  As String = "http://schemas.microsoft.com/mapi/proptag/"
Const TxtWordSubject As String = "Office:"
Dim OutTable As Outlook.Table
Dim OutRow As Outlook.Row
Dim OutEmail As Outlook.MailItem
Dim OutApp As Outlook.Application: Set OutApp = New Outlook.Application
Dim CounterEmails As Long
Dim TotalEmails As Long
Dim TxtFilter As String: TxtFilter = "@SQL=" & Chr(34) & TxtTag & "0x0037001E" & Chr(34) & " ci_phrasematch '" & TxtWordSubject & "'"
Dim TxtCourse As String
Dim DteReport As Date

Set OutTable = OutApp.Session.GetDefaultFolder(olFolderInbox).GetTable()
    TotalEmails = OutTable.GetRowCount
    For CounterEmails = 1 To TotalEmails
    Set OutRow = OutTable.GetNextRow
    DteReport = OutRow("LastModificationTime")
    TxtCourse = OutRow("Subject")
    'Define a string for the EntryId
    Dim entryID As String
    'get EntrId
    entryID = OutRow("EntryID")
    'define a MailItem
    Dim mi As MailItem
    'Get the MailItem from the ID
    Set mi = OutApp.Session.GetItemFromID(entryID)
    'do something with the mail item
    TxtCourse = Right(TxtCourse, Len(TxtCourse) - Len(TxtWordSubject))
    Next CounterEmails

End Sub



回答2:


To extract outlook Emails to excel use the following code in an excel file with reference Microsoft Outlook View Control and MS Outlook 16.0 Object library.

Code:

Sub GetFromOutlook()
Dim OutlookApp As Outlook.Application
Dim OutlookNamespace As Namespace
Dim wb As Workbook, ws As Worksheet
Dim Folder As MAPIFolder
Dim OutlookMail As Variant
Dim i As Integer
Set wb = ThisWorkbook
Set ws = wb.Sheets("Mail")
Set OutlookApp = New Outlook.Application
Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
Set Folder = OutlookNamespace.GetDefaultFolder(olFolderInbox).GetTable(TxtFilter)    
i = 1

For Each OutlookMail In Folder.Items
'here you can update the condition to which it should be extracted

    If OutlookMail.ReceivedTime > ws.Range("D" & i).Value And OutlookMail.Subject <> ws.Range("B" & i).Value Then 
            ws.Range("B1").Offset(i, 0).Value = OutlookMail.Subject
        ws.Range("C1").Offset(i, 0).Value = OutlookMail.ReceivedTime
        ws.Range("D1").Offset(i, 0).Value = OutlookMail.ReceivedTime
        ws.Range("E1").Offset(i, 0).Value = OutlookMail.SenderName
        ws.Range("F1").Offset(i, 0).Value = OutlookMail.Body
        i = i + 1
    End If
Next OutlookMail

Set Folder = Nothing
Set OutlookNamespace = Nothing
Set OutlookApp = Nothing

End Sub


来源:https://stackoverflow.com/questions/49135547/obtain-sender-and-emailbody-properties

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!