Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Outlook - Extract values from email body and remove lines

Status
Not open for further replies.

HuanManwe

Technical User
Sep 20, 2013
4
IE
Hi all,

I need to create a macro to reply to Outlook mails with the same emails received but changing a few things:

1.- I need to extract the .To information from the email body. At the moment I only get ONE email address:
Code:

Code:
If InStr(1, vText(i), "@") Then
sText = Replace(vText(i), "mailto:", "")

I need to extract all emails from the text and add all those emails to the .To section of the reply.

2.- After point 1 the macro needs to remove the first few lines of the email body. From the words "CONFIRMATION MAIL" to "Good Morning". I get the position for those two strings but I have no idea about how to remove that part of the body before I send the reply.

Could anyone help me? I think this should be easy for a senior developer.

Thank you in advance.
 
Could you show an example of your e-mail body you work with? (change names and e-mails to protect the innocent :) )

Have fun.

---- Andy
 
Code:
CONFIRMATION MAIL


whatever@something.com; etwas@deutsch.com; algo@hispano.es
About the analysis given:

Good Morning

Could you please confirm these analysis?

I'm trying several things at the same time so maybe my code is messy and duplicated.

Thanks for your help.

Code:
Sub AA7()
Dim Tbl As Table
Dim Reply As Outlook.MailItem
Dim Original As Outlook.MailItem
Dim vText As Variant
Dim sText As String
Dim first As String
Dim last As String
Dim vAddr As Variant
Dim sAddr As String
Dim mails() As Variant
Dim CCmail As String
Dim i, k As Long
Dim x As Long
If Application.ActiveExplorer.selection.Count = 0 Then
MsgBox "No Items selected!", vbCritical, "Error"
Exit Sub
End If
Set Original = Application.ActiveExplorer.selection(1)

'Tbl.Columns(i).Delete


For Each olItem In Application.ActiveExplorer.selection
sText = olItem.body
first = InStr(1, sText, "CONFIRMATION MAIL")
last = InStr(1, sText, "Hello")
x = 0
x = last - first
MsgBox ("x = last - first so x = " & x)



vText = Split(sText, Chr(13))
For i = 1 To UBound(vText)
    If InStr(1, vText(i), "@") Then
        sAddr = vText(i)
        'vAddr(i) = sAddr
Exit For
    End If
    Next i
If InStr(1, sAddr, "HYPERLINK") Then
vText = Split(sAddr, Chr(34))
For i = 1 To UBound(vText)
If InStr(1, vText(i), "@") Then
vAddr = _
vText(i)


For k = 1 To UBound(vText)
    MsgBox ("vAddr value: " & vAddr)
    'mails(k) = vAddr
Next k


'mails(i) = vText(i)
End If
'line 37

If InStr(1, vText(i), "@") Then
sText = Replace(vText(i), "mailto:", "")

End If
Exit For
Next i
End If



Set Reply = Original.Reply
Reply.Importance = olImportanceHigh
'Reply.Attachments.Add Original
Reply.Subject = "**PLEASE READ**  " & Original.Subject
Reply.HTMLBody = Original.HTMLBody

'Code to add the original attachments to the reply mail
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set fldTemp = fso.GetSpecialFolder(2) ' TemporaryFolder
   strPath = fldTemp.Path & "\"
   For Each objAtt In Original.Attachments
      strFile = strPath & objAtt.FileName
      objAtt.SaveAsFile strFile
      Reply.Attachments.Add strFile, , , objAtt.DisplayName
      fso.DeleteFile strFile
   Next
   Set fldTemp = Nothing
   Set fso = Nothing



'ANOTHER WAY TO TRY TO EXTRACT EMAIL ADDRESSES FROM THE BODY
 Dim arr() As String
 arr = Split(Original.body, vbCrLf)
 Dim mailto As String
 mailto = ""
  Dim j As Integer
 j = 0
 Do Until j > 12
     If InStr(StrConv(arr(j), vbLowerCase), "@") Then mailto = mailto + arr(j)
     With Reply
     .CC = CCmail
   
     j = j + 1
     End With
 Loop


sAddr = Replace(vAddr, "mailto:", "")
Set olOutMail = olItem.Forward
With olOutMail
.To = vAddr
End With

    
With Reply
'Reply.To = mailto
'Reply.ReplyRecipients.Add mailto
'.Recipients.Add sAddr
.To = sText
'line 114
'MsgBox ("sText at line 115 is: " & sText)
.CC = CCmail
.SentOnBehalfOfName = "myemail@whatever.com"
End With

'Replay.CC = CCcopy


MailText = Reply.HTMLBody
Reply.HTMLBody = MailText
Reply.Display
'Reply.Send

Next olItem

CleanUp:

Set Replay = Nothing
Set Original = Nothing
Set olItem = Nothing
Set olOutMail = Nothing

End Sub
 
Function GetCurrentItem() As Object
    Dim objApp As Outlook.Application
         
    Set objApp = Application
    On Error Resume Next
    Select Case TypeName(objApp.ActiveWindow)
        Case "Explorer"
            Set GetCurrentItem = objApp.ActiveExplorer.selection.Item(1)
        Case "Inspector"
            Set GetCurrentItem = objApp.ActiveInspector.CurrentItem
    End Select
     
    Set objApp = Nothing
End Function
 
Sub CopyAttachments(objSourceItem, objTargetItem)
   Set fso = CreateObject("Scripting.FileSystemObject")
   Set fldTemp = fso.GetSpecialFolder(2) ' TemporaryFolder
   strPath = fldTemp.Path & "\"
   For Each objAtt In objSourceItem.Attachments
      strFile = strPath & objAtt.FileName
      objAtt.SaveAsFile strFile
      objTargetItem.Attachments.Add strFile, , , objAtt.DisplayName
      fso.DeleteFile strFile
   Next
 
   Set fldTemp = Nothing
   Set fso = Nothing
End Sub
 
To retrieve e-mail addresses from your body, consider this:

Code:
Dim strBody As String
Dim aryLines() As String
Dim aryEMail() As String
Dim i As Integer
Dim e As Integer

strBody = "CONFIRMATION MAIL" & vbNewLine & vbNewLine & _
    "whatever@something.com; etwas@deutsch.com; algo@hispano.es" & vbNewLine & _
    "About the analysis given: " & vbNewLine & vbNewLine & _
    "Good Morning" & vbNewLine & vbNewLine & _
    "Could you please confirm these analysis?"

Debug.Print strBody

aryLines = Split(strBody, vbNewLine)

For i = LBound(aryLines) To UBound(aryLines)
    If InStr(aryLines(i), "@") Then
        aryEMail = Split(aryLines(i), ";")
        For e = LBound(aryEMail) To UBound(aryEMail)
            Debug.Print Trim(aryEMail(e))
        Next e
    End If
Next i

And Debug.Print gets you:[tt]
whatever@something.com
etwas@deutsch.com
algo@hispano.es
[/tt]

Have fun.

---- Andy
 
As far as getting everything from your body after the words "Good Morning":

Code:
Dim strBody As String
Dim aryLines() As String
Dim i As Integer
Dim blnGetIt As Boolean

strBody = "CONFIRMATION MAIL" & vbNewLine & vbNewLine & _
    "whatever@something.com; etwas@deutsch.com; algo@hispano.es" & vbNewLine & _
    "About the analysis given: " & vbNewLine & vbNewLine & _
    "Good Morning" & vbNewLine & vbNewLine & _
    "Could you please confirm these analysis?"

Debug.Print strBody

aryLines = Split(strBody, vbNewLine)

For i = LBound(aryLines) To UBound(aryLines)
    If blnGetIt Then
        Debug.Print aryLines(i)
    End If
    If aryLines(i) = "Good Morning" Then
        blnGetIt = True
    End If
Next i

Have fun.

---- Andy
 
Thank you for your reply.

This code looks nice but I have a problem:

1.- The emails within the email body are always different, and sometimes they're from 2 email addresses to about 10 email addresses.

2.- As the content of the email is always a bit different I can't use "Dim strBody As String" as you did. I need to use Reply.Body or Reply.HTMLbody to get the text within the email body and then work with those lines.

But it's a good idea to split the email addresses as I receive them in one line and they're easier to manage if they're separated line by line.

Thank you very much once more for your help. I've found references all over internet about adding text at the beginning or the end of email bodies, or how to replace a string for another, but I've never find the solution for a case like mine and I think it would be very useful for others too.

Thanks!
 
1. With the code I gave you, you should be able to pick any e-mail address from anywhere in the body of the e-mail, as long as it is in the format of an e-mail address (with the @ in it) no matter if there is 1, 2, 15, or 200 of them. :)

2. You would not use "Dim strBody As String" as in my approach. This was only to demonstrate how I create (an example of) a body of a message 'on-the-fly' and how to deal with it in the code. You would use your [tt]sText[/tt] instead, because that's where you keep the body of the message (I think)

Have fun.

---- Andy
 
With the code you gave me to extract the email addresses from the body I have a problem: it works with your code, but the problem is that I can't get the same format from the real emails. For example, if I get the text from the email body using MailItem.Body and parse it with your code I get something like:
HYPERLINK "mailto:whatever@something.com";"whatever@something.com"
or something like that.

If I use MailItem.HTMLBody is even worse because it also extracts other lines that aren't useful.

Thanks.
 

I showed you how to deal with e-mail addresses (and stuff) in a simple text. As far as HTMLBody and other formats, somebody else with more knowledge about it needs to show you how to parse it in order to help you.

Sorry :-(

Have fun.

---- Andy
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top