Sub GetEmailFromBody() ' ------------------------------------------------ ' --- You may use and/or change this code freely ' --- provided you keep this message ' --- ' --- Description: ' --- Extracts first found email address from body ' --- (used to extract email address from ' --- error messages/returned email) ' --- Runs on all items in current folder ' --- ' --- By Max Flodén 2006 - http://www.tjitjing.com ' ------------------------------------------------ Dim myOlApp As New Outlook.Application Dim myNameSpace As Outlook.NameSpace Dim mySelection As Selection Dim myItem As Object Dim myMailItemLog As Outlook.MailItem Dim myFolder As Outlook.MAPIFolder Dim strContactFolderName As String 'Directly under Public Folders\All Public Folders Dim strNewsletterCategoryName As String Dim strMailItemSender As String Dim strMailTo As String Dim intMessageCount As Integer Dim bolDebug As Boolean 'If true no emails will be sent Dim bolOnly550 As Boolean 'Only extract email addresses that are 'user not found' (#550) etc. Dim strTemp As String Set myNameSpace = myOlApp.GetNamespace("MAPI") 'Debug settings bolDebug = True 'Ask to continue - start warning intRes = MsgBox("This macro will go thru all items in folder." & vbCrLf & "Would like to extract only addresses that have 'user not found'?", vbYesNoCancel + vbQuestion, "Get Email from Body") If intRes = vbCancel Then Exit Sub ElseIf intRes = vbYes Then bolOnly550 = True Else bolOnly550 = False End If 'Create a new email to use as log file Set myMailItemLog = myOlApp.CreateItem(olMailItem) myMailItemLog.Recipients.Add (myNameSpace.CurrentUser) myMailItemLog.Subject = "Email from Body - " & Now() myMailItemLog.BodyFormat = olFormatPlain myMailItemLog.Body = Now() & " Starting..." & vbCrLf & vbCrLf 'Go thru all items in folder intMessageCount = 0 intMsgCount_Error = 0 For Each myItem In myOlApp.ActiveExplorer.CurrentFolder.Items If Not TypeName(myItem) = "ReportItem" And Not TypeName(myItem) = "MailItem" Then 'Errorlog If bolDebug Then myMailItemLog.Body = myMailItemLog.Body & "ERROR - MESSAGE TYPE IS NOT REPORTITEM OR MAILITEM." & vbCrLf myItem.UnRead = True intMsgCount_Error = intMsgCount_Error + 1 Else 'Check type is 550 - user not found/inactive etc '2007-03-27 removed 554 error If bolOnly550 And _ (InStr(myItem.Body, "550") = 0) And _ (InStr(myItem.Body, "unknown user") = 0) And _ (InStr(myItem.Body, "user unknown") = 0) And _ (InStr(myItem.Body, "no mailbox here by that name") = 0) And _ (InStr(myItem.Body, "no such user") = 0) And _ (InStr(myItem.Body, "bad address") = 0) And _ (InStr(myItem.Body, "Host or domain name not found") = 0) And _ (InStr(myItem.Body, "e-mail account does not exist") = 0) Then If bolDebug Then myMailItemLog.Body = myMailItemLog.Body & "ERROR - NOT 550 OR Host or domain name not found MESSAGE." & vbCrLf myItem.UnRead = True intMsgCount_Error = intMsgCount_Error + 1 Else 'Extract email address from body intPos = InStr(myItem.Body, "@") If intPos = 0 Then 'No email address found If bolDebug Then myMailItemLog.Body = myMailItemLog.Body & "ERROR - NO EMAIL ADDRESS FOUND IN MESSAGE." & vbCrLf myItem.UnRead = True intMsgCount_Error = intMsgCount_Error + 1 Else 'Get right of @ intPos_Space = InStr(intPos, myItem.Body, " ") intPos_Bracket = InStr(intPos, myItem.Body, ">") If (intPos_Space < intPos_Bracket) Or (intPos_Bracket = 0) Then intPos_Temp = intPos_Space Else intPos_Temp = intPos_Bracket End If strTemp = Left(myItem.Body, intPos_Temp - 1) 'Get left of @ intPos_Space = InStrRev(strTemp, " ", -1) intPos_Bracket = InStrRev(strTemp, "<", -1) If (intPos_Space > intPos_Bracket) Or (intPos_Bracket = 0) Then intPos_Temp = intPos_Space Else intPos_Temp = intPos_Bracket End If strTemp = Mid(strTemp, intPos_Temp + 1) 'Write to log myMailItemLog.Body = myMailItemLog.Body & strTemp & vbCrLf myItem.UnRead = False intMessageCount = intMessageCount + 1 End If End If End If Next 'Done - write to log and show done message myMailItemLog.Body = myMailItemLog.Body & vbCrLf & Now() & " Done. Email addresses extracted: " & intMessageCount & ". Email addresses NOT extracted: " & intMsgCount_Error & "." myMailItemLog.Display MsgBox Now() & " Done. Email addresses extracted: " & intMessageCount & ". Email addresses NOT extracted: " & intMsgCount_Error & ".", vbInformation, "Done" End Sub