Outlook macro to extract address from body of email into CSV

Wondering if anyone can help me with an Outlook macro?

I need a macro that will run against the selected folder.
The macro should review all items with the subject "[Postmaster] Email Delivery Failure]"
It should then extract the email address found within the body of the email and save it to a csv file

Each item found with an email address in the body should be appended to the csv file.

Many thanks!
Antonio KingIT ManagerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill PrewCommented:
How can the email address be located in the body of the email?  Please provide sample(s).

Is this a typo for the subject, or does it look exactly like that?

"[Postmaster] Email Delivery Failure]"


»bp
0
Antonio KingIT ManagerAuthor Commented:
Hi bill
That is a typo. Well spotted
The subject is "[Postmaster] Email Delivery Failure"

A typical email body would look like this...
This is a delivery failure notification message indicating that an email you addressed to email address :
-- email@domain.com

could not be delivered. The problem appears to be :
-- Recipient email server rejected the message

Additional information follows :
-- delivery error: dd This user doesn't have a yahoo.com account (email@domain.com) [0] - mail.server.com

This condition occurred after 1 attempt(s) to deliver over a period of 0 hour(s).

If you sent the email to multiple recipients, you will receive one of these messages for each one which failed delivery,  otherwise they have been sent.
0
Bill PrewCommented:
This seems to work here, give it a try there...

Sub ExtractEmail()
    Dim objItem As Outlook.MailItem
    Dim strEmail As String
    Dim objRegex As Object
    Dim intFile As Integer
    Dim strFile As String

    ' Define full path to email log file
    strFile = "c:\temp\EE29100783.txt"

    ' Build REGEX that will be used to pull email address out of body text
    Set objRegex = CreateObject("VbScript.RegExp")
    objRegex.Pattern = "you addressed to email address : -- (.*)\s"
    objRegex.Global = False
    objRegex.IgnoreCase = True

    ' Open log file for appended output
    intFile = FreeFile
    Open strFile For Append As #intFile

    ' Look at each email in the selected folder
    For Each objItem In Application.ActiveExplorer.Selection
        ' Make sure it's an email item
        If objItem.Class = olMail Then
            ' Make sure it has the subject we want
            If objItem.Subject = "[Postmaster] Email Delivery Failure" Then
                ' Can we find the email address we want?
                If objRegex.Test(objItem.Body) Then
                    ' Yes, write it to the log file
                    With objRegex.Execute(objItem.Body)
                        Print #intFile, .Item(0).SubMatches(0)
                    End With
                End If
            End If
        End If
    Next

    Close #intFile

End Sub

Open in new window


»bp
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.