Extract values from email body

HuanManwe

New Member
Joined
Sep 20, 2013
Messages
3
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:
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.
 
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
 
Upvote 0

Forum statistics

Threads
1,226,908
Messages
6,193,611
Members
453,810
Latest member
Gks77117

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top