PDA

View Full Version : [SOLVED:] auto reply (with template) to email address contained within body of email



JamieFoote
07-15-2015, 07:40 AM
Hi guys/girls,

I need to use VBA to send an automatic reply to an email address contained within the body of an email that I have received.

The received email looks like this:

Thank you for registering with [company name]. You used the following email address: exampleemailaddress

To verify your email address please click on this link:

[link]

This link will expire in two days.

-------------------------------------------------------------------------

I receive around 50 of these emails per day and so can't just reply to each email address manually.

I have searched hi and lo across all of the far reaching corners of the internet with no luck as to example code and so my last hope is to ask the members of this forum if they can offer any help on the matter.

I thought that this would be a common and perhaps quite typical macro but can't find examples anywhere.

Kind Regards,
JamieF

gmayor
07-15-2015, 09:54 PM
It is relatively easy to read the body of an e-mail and extract data from it. This is covered at http://www.gmayor.com/extract_data_from_email.htm, however in this instance you don't want to send the information to Excel, but use it to create a reply to the message, as shown below. You would run the main script from a rule to identify the messages, but I have included a test macro that you can run manually with a message selected.



Option Explicit

Sub SendAutoReply(olItem As MailItem)
Dim vText As Variant
Dim sText As String
Dim sAddr As String
Dim vItem As Variant
Dim i As Long
Dim olOutMail As MailItem
Const strTemplate As String = "C:\Path\autoreply.oft" 'Your autoreply template

'Process the message
With olItem
sText = olItem.Body
vText = Split(sText, Chr(13))

'Check each line of text in the message body
For i = UBound(vText) To 0 Step -1
If InStr(1, vText(i), "You used the following email address:") > 0 Then
vItem = Split(vText(i), Chr(58))
sAddr = Trim(vItem(1))
Set olOutMail = CreateItemFromTemplate(strTemplate)
With olOutMail
.To = sAddr
.Display '(change to .Send after testing)
End With
Exit For
End If
Next i
End With
lbl_Exit:
Set olOutMail = Nothing
Exit Sub
End Sub

Sub TestMacro()
Dim olMsg As MailItem
On Error Resume Next
Set olMsg = ActiveExplorer.Selection.Item(1)
SendAutoReply olMsg
lbl_Exit:
Exit Sub
End Sub

JamieFoote
07-16-2015, 02:57 AM
Thank you so much for taking the time to reply. The solution is exactly what I was looking for.

You have helped me so much!!

Kind Regards,
JamieF

JamieFoote
07-23-2015, 09:04 AM
Dear Graham,

Please may you explain to me how to get this to run in Outlook 2013. I have opened the editor (alt +f11), copied in the code and made the suggested changes that you included
However the rule doesn't seem to be running.

There must be something I have missed. I have included a couple of screenshots but can provide more if needed.

1398413985

Kind Regards,
JamieF

gmayor
07-23-2015, 08:34 PM
The code should be in an ordinary module and not ThisOutlookSession (from the VBA editor > Insert > Module). Move the code. Whether this will prevent it from running I cannot say.
Are your rule parameters correct?
Does the macro run from the Test macro?

JamieFoote
07-24-2015, 02:47 AM
I think that my rule parameters are correct as I have multiple rules currently running in Outlook 2013 so I am familiar with how to create them.

The TestMacro runs but the send autoreply sub is not appearing as an option to run.

I have the option 'run a script' selected as an action in my rule and the chosen script is Project1.SendAutoReply . But nothing happens when I run the rule.

I have included some more screenshots.

139891399013992

Kind Regards,
Jamie

gmayor
07-24-2015, 05:08 AM
The test macro runs the SendAutoReply macro, which won't appear as a macro to run as it requires a parameter to be supplied. It will however be listed as a script to attach to a rule. That rule will run when a new message that meets the parameters of the rule arrives in the Inbox. I have tested it with a message derived from your original post and it works as stated.

The rule I used was

13993

JamieFoote
07-24-2015, 06:34 AM
Got it working. Successfully pulled the email address out of the body and sent the autoreply template! Thankyou so much for all the help Graham.

Kind Regards,
JamieF