Outlook VBA forward the selected email to original sender’s email ID (including the email used in TO, CC Field) from the email chain

rajeshmuthu

New Member
Joined
Mar 26, 2018
Messages
1
Hi All,

Can someone help me to achieve this VBA? I am not very familiar with VBA.

I would like to find the original senders email address from the email chain of “From:” Field (not from our inside organization emails like “@test.net”, “@testsupport.com” – check the attached image boxed in Green) and get it’s TO, CC fields on that email (including the inside organization emails if available on those TO & CC fields – Boxed in Pink). Then forward that selected email to the original senders email (need to add in TO field – Boxed in Green) and the remaining email address in CC field (Boxed in Pink).

It will be much appreciated. Thank you.

I found the below codes online and it is working fine. But it is getting email addres from "From"" field only. Also I have no idea about how to change this code to get the TO, CC field of the orginal email and how to set to forward the selected email. if someone help me, It will be much appreciated and will save lot of time on my end. Thank you.

VBA Code:
Sub GetSenderFromSelectedEmailChainSource()

Dim olApp As Outlook.Application

Dim selectedEmail As Object

Dim olMailItem As Outlook.MailItem

Dim senderEmail As String

Dim internalDomainFound As Boolean



Set olApp = New Outlook.Application

Set selectedEmail = olApp.ActiveExplorer.Selection(1) ' Get the selected email

Set olMailItem = selectedEmail



If TypeOf olMailItem Is Outlook.MailItem Then

' Get the source code of the selected email

Dim sourceCode As String

sourceCode = olMailItem.HTMLBody



' Use regular expressions to find sender email addresses

Dim regex As Object

Set regex = CreateObject("VBScript.RegExp")

regex.Global = True

regex.IgnoreCase = True



' Define the pattern to match email addresses

regex.pattern = "\b[A-Za-z0-9._%+-][EMAIL='+@[A-Za-z0-9.-]+\.%5bA-Z|a-z%5d%7b2,7%7d\b']+@[A-Za-z0-9.-]+\.[A-Z|a-z]{2,7}\b[/EMAIL]"



' Find matches in the source code

Dim matches As Object

Set matches = regex.Execute(sourceCode)



' Iterate through matches to find the sender's email

Dim match As Object

For Each match In matches

senderEmail = match.Value

internalDomainFound = IsInternalDomain(senderEmail)



If Not internalDomainFound Then

Debug.Print "Sender Email from Source: " & senderEmail

Exit For

End If

Next match



If internalDomainFound Then

Debug.Print "No suitable sender email found in the source."

End If

End If



Set olApp = Nothing

Set selectedEmail = Nothing

Set olMailItem = Nothing

End Sub



Function IsInternalDomain(emailAddress As String) As Boolean

' Define your internal domain names here

Dim internalDomains() As String

internalDomains = Split("@test.net,@testsupport.com", ",")



Dim domain As String

domain = Right(emailAddress, Len(emailAddress) - InStr(emailAddress, "@"))



Dim i As Integer

For i = LBound(internalDomains) To UBound(internalDomains)

If LCase(domain) = LCase(internalDomains(i)) Then

IsInternalDomain = True

Exit Function

End If

Next i



IsInternalDomain = False

 End Function
 

Attachments

  • Image.jpg
    Image.jpg
    64 KB · Views: 8

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK

Forum statistics

Threads
1,224,811
Messages
6,181,081
Members
453,021
Latest member
Justyna P

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