Extract text string from undeliverable email body to excel

后端 未结 5 789
没有蜡笔的小新
没有蜡笔的小新 2021-01-13 12:50

I need some help on Outlook VBA.

I am trying to write a macro in Outlook for extracting the email address from each individual undeliverables email body.

Th

5条回答
  •  春和景丽
    2021-01-13 13:20

    sI have been having exactly the same issue. All of the NDR messages I am dealing with are of the class "REPORT.IPM.Note.NDR" and the method I found for obtaining the original recipient was pieced together from a number of these sorts of posts and questions that I've been trawling through!

    I am using the PropertyAccessor.GetProperty method against the ReportItem to obtain the PR_DISPLAY_TO property value from the header information of the ReportItem.

    In VBA, I am using the MAPI namepace and looping through the olItems collection of a given folder containing the report messages. I'm running this from Access as my database front-end is built that way, but I would imagine you can probably run it from within Outlook VBA (but don't hold me to that).

    Dim olApp As Outlook.Application
    Dim OlMapi As Outlook.NameSpace
    Dim olFolder As Outlook.MAPIFolder
    Dim olMail As Outlook.ReportItem
    Dim OlItems As Outlook.Items
    
    Set olApp = CreateObject("Outlook.Application")
    Set OlMapi = olApp.GetNamespace("MAPI")
    Set olFolder = OlMapi.Folders("SMTP-ADDRESS-FOR-YOUR-MAILBOX").Folders("Inbox").Folders("NAME-OF-SUBFOLDER_CONTAINING-NDR-REPORTS")
    Set OlItems = olFolder.Items
    
    If OlItem.Count > 0 Then
        For Each olMail In OlItems
            strEmail = olMail.PropertyAccessor.GetProperty("http://schemas.microsoft.com/mapi/proptag/0x0E04001E")
            'DO WITH strEmail AS REQUIRED   
            DoEvents
        Next
    End If
    

    The returned value from that MAPI property could be a semicolon delimited list where there are multiple recipients, so you could check for ';' in the returned string and then split into an array and iterate through to get each individual address, but in my case, there is only ever one recipient so I didn't need to over complicate it. It also may be a display name when the original recipient is a contact, so this may be a shortcoming for some, but again in my case, that's not a factor.

    This is just a snippet of a bigger function so you will need to amend and integrate it to your needs, and obviously replace or amend the placeholders for the mailbox and subfolder values.

    The intention is currently to also extract the NDR reason code so that I can automate removal of email addresses from our database where the reason is because the mailbox does not exist, so referring only to ReportItem object - This likely won't work for NDR emails which are not of that type, as I would image thoe MAPI properties are not available, however I have found in practice that all of the NDR messages come back like this as we are using Exchange Online.

提交回复
热议问题