Extract Email Addresses into Excel

Hi Experts,

what I am looking to do is extract an email address from the body of an email received in outlook 2003. I want to export the email address into an Excel spread sheet if possible. all the emails that we receive are in the format of the attachment

they all look identical (except for the information provided / details)

let me know, thanks
Email-Extract.PNG
LVL 2
peggiegregAsked:
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.

Christopher Jay WolffWiggle My Legs, OwnerCommented:
It looks like you're posting real info.  Probably not a good idea.
0
ProfessorJimJamCommented:
i would say, there is No easy solution for this. i would not leave this question until it is marked as Neglected :-), so here is the code that I used long time ago.

just to mention that i am not the original author of this the code it was from some one at inpec DOT fr ,

in your microsoft outlook, insert a empty module and then paste this code there, then run the macro "GetEmail()"  and it will take couple of minutes, do not worry if it freezes  if your mailbox is too big.

then it will open an excel file with all emails extracted from the body of the emails.

let me know how it goes. i tested it in outlook 2010 and it worked for me.

Dim eMails(), noms() As String
Dim EmailFromBody As Boolean
Private Declare Function GetTempPath Lib "kernel32" Alias "GetTempPathA" (ByVal nBufferLength As Long, ByVal lpBuffer As String) As Long



Sub GetEmail()
    Set rep = Outlook.Application.ActiveExplorer.CurrentFolder

    reponse = MsgBox("Do you want to extract emails from messages ?", vbYesNoCancel)
    If reponse = vbCancel Then
      Exit Sub
    ElseIf reponse = vbYes Then
      EmailFromBody = True
    Else
      EmailFromBody = False
    End If
    ReDim Preserve eMails(1), noms(1)
    eMails(1) = ""
    noms(1) = ""

    GetEmailFromFolder rep
    If eMails(1) <> "" Then
        NomFichier = GetTempDir() & "/emails.xls"
        Close #1
        Open NomFichier For Output As #1
            For i = 1 To UBound(eMails)
                Print #1, AfficheEmail(noms(i), eMails(i))
            Next
        Close #1
        MsgBox UBound(eMails) & " emails found in  " & rep, vbInformation, "Done"
        OpenExcel (NomFichier)
    Else
        MsgBox "No email found " & rep, vbInformation, "Done"
    End If

End Sub
Function AfficheEmail(Nom, email)
    email = Replace(email, "'", "")
    If Nom = "" Or Nom = "body" Then
       Nom = email
    End If
    AfficheEmail = email + vbTab + Nom + vcrlf
End Function

Sub GetEmailFromFolder(MyFolder)
    Dim myItemRec, MyItem As Object
    Dim myMailItem As Outlook.MailItem
  
    For Each MyItem In MyFolder.Folders
            GetEmailFromFolder MyItem
    Next

    rep = Replace(MyFolder.FolderPath, "\", vbTab)
  
    For Each MyItem In MyFolder.Items
        If TypeName(MyItem) = "MailItem" Then
   
            For Each myItemRec In MyItem.Recipients
                addMail myItemRec.Name & vbTab & "dest" & rep, myItemRec.Address
            Next
           
            addMail MyItem.SenderName & vbTab & "exp" & rep, MyItem.SenderEmailAddress
            
           If EmailFromBody Then findMail MyItem.body, rep
        End If
    Next
End Sub

Sub addMail(Nom, email)
    email = TrimEmail(email)
    Nom = Trim(Nom)
    If email <> "" And InStr(email, "@") > 0 And InStr(email, ".") > 0 Then
       
        Find = UBound(Filter(eMails, email, True, vbTextCompare))
        If eMails(1) = "" Then
            eMails(1) = email
            noms(1) = Nom
        ElseIf Find = -1 Then
           
            ReDim Preserve eMails(UBound(eMails) + 1)
            ReDim Preserve noms(UBound(noms) + 1)
            eMails(UBound(eMails)) = email
            noms(UBound(noms)) = Nom
        Else
        
            If Len(Nom) > Len(noms(Find)) And InStr(Nom, "@") = 0 Then
                noms(Find) = Nom
            End If
        End If
    End If
End Sub
Sub findMail(body, rep)
    at = InStr(body, "@")
    Do While at > 1
        D = at - 1
        Do While carOk(Mid(body, D, 1))
            D = D - 1
            If D = 0 Then
               Exit Do
            End If
        Loop
        F = at + 1
        Do While carOk(Mid(body, F, 1))
            F = F + 1
            If F = Len(body) Then
               Exit Do
            End If
        Loop
        If D < at - 3 And F > at + 4 Then
            addMail vbTab & "corps" & rep, Mid(body, D + 1, F - D - 1)
        End If
        at = InStr(at + 1, body, "@")
    Loop
End Sub
Function carOk(c)
    If c = "." Or c = "-" Or c = "_" Or (c >= "0" And c <= "9") Or (c >= "A" And c <= "Z") Or (c >= "a" And c <= "z") Then
        carOk = True
    Else
        carOk = False
    End If
End Function
Function carOkDebut(c)
    If c = "-" Or c = "_" Or (c >= "0" And c <= "9") Or (c >= "a" And c <= "z") Then
        carOkDebut = True
    Else
        carOkDebut = False
    End If
End Function
Function carOkFin(c)
    If (c >= "a" And c <= "z") Then
        carOkFin = True
    Else
        carOkFin = False
    End If
End Function
Function TrimEmail(email_ini)
  email = Trim(LCase(email_ini))
  D = Len(email)
  For i = 1 To D
    If Not carOkDebut(Left(email, 1)) Then
      email = Mid(email, 2, Len(email) - 1)
    Else
      Exit For
    End If
  Next i
  D = Len(email)
  For i = 1 To D
    If Not carOkFin(Right(email, 1)) Then
      email = Mid(email, 1, Len(email) - 1)
    Else
      Exit For
    End If
  Next i
  TrimEmail = email
End Function
Sub OpenExcel(FileName)
    Set xls = CreateObject("Excel.Application")
    xls.Workbooks.Open FileName
    xls.Visible = True
    Exit Sub
End Sub
Function GetTempDir() As String
    Dim buffer As String * 256
    Dim Length As Long
    Length = GetTempPath(Len(buffer), buffer)
    GetTempDir = Left(buffer, Length)
End Function

Open in new window

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
zuleaCommented:
ProfessorJimJam,
I tried your code. It works great. I noticed that you said it extracts emails from the message body. When I run it I get emails from the TO field but nothing from the body of the email. Is that correct?
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

zuleaCommented:
ProfessorJimJam,
One other question. what is column "C"? It has entries like exp, corps, dest.

Thanks
0
ProfessorJimJamCommented:
It takes emails from body.

Try writing an email in a body then send it to your self and then when you received that email run the code and you will see that the email you wrote in body is extracted in excel
0
peggiegregAuthor Commented:
brilliant, worked exactly as I wanted. thank you ProfessorJimJam
0
ProfessorJimJamCommented:
You are welcome. I am glad I was able to help
0
Richard KreidlSoftware DeveloperCommented:
Where in your code do you designate the name of the folder in which the emails reside in?

I have a special folder where a rule puts the undeliverable mail in that folder.

Normally returned undelivered emails appear in the "Inbox".
0
ProfessorJimJamCommented:
i answered to this in your original question. this thread is closed. please see my comments there.
0
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
Internet / Email Software

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.