Consulting

Results 1 to 8 of 8

Thread: auto reply (with template) to email address contained within body of email

  1. #1

    auto reply (with template) to email address contained within body of email

    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

  2. #2
    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
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  3. #3
    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

  4. #4
    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.

    outlook1.jpgoutlook 2.jpg

    Kind Regards,
    JamieF

  5. #5
    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?
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  6. #6
    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.

    outlook2.jpgOutlook3.jpgoutlook1.jpg

    Kind Regards,
    Jamie
    Attached Images Attached Images
    Last edited by JamieFoote; 07-24-2015 at 04:11 AM.

  7. #7
    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

    Rukl.png
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  8. #8
    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

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •