Parse an email folder(Outlook 2010) that contains undelivered or returned email errors

I'm looking for a VBA Word macro to parse the bodies of emails for email addresses and write them to a Word Document separated by comma's as a delimiter.

This would be used to get the addresses out of returned mail failures so they can be removed from our database. I only have about 4000+ failures.

thanks
LVL 2
Richard KreidlSoftware DeveloperAsked:
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.

ProfessorJimJamCommented:
I am not sure if you could get a better answer than the one i already provided for another similar question.

Please see this answered thread in EE http://www.experts-exchange.com/questions/28693672/Extract-Email-Addresses-into-Excel.html
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
ProfessorJimJamCommented:
RE : your question,

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".

the designation is from the selection on the desired mailbox.

for example if you select your INBOX folder then you run this code. it will extract emails from Inbox only.

if you have a specific folder then select it first then run the macro it will extract from the selection.

this part of the code
Set rep = Outlook.Application.ActiveExplorer.CurrentFolder

Open in new window

triggers the folder on which the macro should excerpt.  now it depends where is actually your folder, is it sub-folder to the default folder or is it a default folder.

 if you want change that to specific default folder or subfolder then you need to replace the abovementioned line with the below

for example

dim rep as outlook folder

set rep = Application.Session.GetDefaultFolder(olFolderInbox).Folders("your subfolder name")
set  Outlook.Application.ActiveExplorer.CurrentFolder= rep

Open in new window



if your folder is not subfolder or is any default folder then simply remove ".Folders("your subfolder name")"  from the code and if the folder is not INBOXFOLDER Then use one of the below default folders as needed.
2015-09-04-15_39_05-OlDefaultFolders.png

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
Richard KreidlSoftware DeveloperAuthor Commented:
thanks
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
Microsoft Word

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.