Link to home
Start Free TrialLog in
Avatar of bboitano
bboitano

asked on

Customised Auto Reply to Specific Mails

Hi,

I am looking for a solution to auto reply to a message with a customised form. We are not running Exchange Server and are using Outlook as a stand-alone email client.

We recieve queries via email that I would like to be able to reply to with a questionnaire (with drop down tables) if possible. The addin would search the incoming mail for specific text and then reply with the HTML questionnaire.

For instance, if we receive a mail such as

========================
Example Enquiry
========================

Title : Mr
First Name : John
Last Name : Doe
Age : 01/01/01
etc
etc

The addin/macro/solution should search for

=========================
Example Enquiry
=========================

so we know that we are replying to the correct mail and then send a
message such as

Dear Mr Doe,
     ^^^^^^^<- Cribbed from email

Thank you for you enquiry .... yadda yadda yadda please find following
....

And then the standard questionnaire template.

I do have basic VB experience so I think I could tailor a sample
script to fit my purposes if I could find a base to start with. I will also want to put some other customisation options in the code (i.e checking that first letter of surname is a capital, error checking that all fields are completed in the incoming email etc etc)

Many thanks in advance
Avatar of will_scarlet7
will_scarlet7

Here's a possible start:

Sub CustReply()

    Dim oOutlook As Outlook.Application
    Dim oNs As Outlook.NameSpace
    Dim oFldr As Outlook.MAPIFolder
    Dim oMessage As Object
    Dim oReply As Object
    Dim strBody As String

    'Set and pick the Outlook folder to save attachments from
    Set oOutlook = New Outlook.Application
    Set oNs = oOutlook.GetNamespace("MAPI")
    Set oFldr = oNs.PickFolder
    If IsNull(oFldr) Then
        GoTo ReplyCleanUp
    End If
   
    'Scan through each mail item in the selected Outlook folder
    For Each oMessage In oFldr.Items
        If oMessage.body Like "*Example Enquiry*" Then
            Set oReply = oMessage.reply
           
            'Add lines to create that you want in your auto reply
           
            With oReply
                .body = strBody & .body
                .Display
            End With
        End If
    Next oMessage

ReplyCleanUp:

    Set oOutlook = Nothing
    Set oNs = Nothing
    Set oFldr = Nothing
    Set oMessage = Nothing
   
End Sub
Avatar of bboitano

ASKER

will_scarlet7,

Thanks - Unfortunately I'm leaving the office in 5 minutes so don't have time to fully check the code you have given me. But on a cursory glance it would appear to be a little less automated that I'm really looking for.

I can't locate the customising section (for the Dear John Doe etc) and even if I run it automatically after each send and receive I think it would generate replies for all mail as it doesn't appear to have any way of differentiating from mails it has already replied to. I guess we could get around that by just searching the inbox and moving all matching mails to a REPLIED folder.

Then again its just as likely I've missed something in my haste. If I have misunderstood some part of the code, please accpet my apologies.

Anyways - thanks for the effort - I just really wanted to say thanks before I went home

Cheers
Boitano
Boitano,
    Your points are valid in that what I posted does not yet do anything other than find the mails matching your search and reply. Like I said it's just a start. However the room for customization is imense, it just depends on your criteria and how you want to go about it. When you have time to get into it in detail post a reply and I'll try to help you make it more customized for your needs.
will scarlet7,

Ready when you are

Boitano
OK, let's see what we can do...
Are you still there Boitano? I was looking for a way to flag the messages after your reply and I think the best would be to assign them to a Category. Below I have modified the code to include first checking if the message is assignetd to a certain setting a category and if not create a custom reply and assign it to the category:
'**********************************
Sub CustReply()

    Dim oOutlook As Outlook.Application
    Dim oNs As Outlook.NameSpace
    Dim oFldr As Outlook.MAPIFolder
    Dim oMessage As Object
    Dim oReply As Object
    Dim strBody As String

    'Set and pick the Outlook folder to save attachments from
    Set oOutlook = New Outlook.Application
    Set oNs = oOutlook.GetNamespace("MAPI")
    Set oFldr = oNs.PickFolder
    If IsNull(oFldr) Then
        GoTo ReplyCleanUp
    End If
   
    'Scan through each mail item in the selected Outlook folder
    For Each oMessage In oFldr.Items
        If oMessage.Body Like "*Example Enquiry*" _
            And oMessage.Categories <> "CstRep" Then 'And IsNull(oMessage.Categories)
            Set oReply = oMessage.Reply
           
            'Add lines to create that you want in your auto reply
           
            With oReply
                .Body = strBody & .Body
                .Display
            End With
            oMessage.Categories = "CstRep"
            oMessage.Save
        End If
    Next oMessage

ReplyCleanUp:

    Set oOutlook = Nothing
    Set oNs = Nothing
    Set oFldr = Nothing
    Set oMessage = Nothing
   
End Sub

'If myinsp.Class = olMail Then
'    myinsp.Categories = "Test"
'End If
'**********************************
We can work more on this, I just wanted to give it to you now so you can see if this will work for you.

God bless!

Sam.
Hi Sam,

Righty ho then.  We're definitely making progress :) Looks like a category or moving it to a new folder is the way to go. Either way a simple rule should be able to handle this just by searching mail on arrival. On reflection, we could just run a rule that moves them to a folder and marks them as a certain category which would then provide the necessary search criteria for the script. For instance :

Run rule that checks for presence of "Example Enquiry" and if true move to Example Inquiry Folder and mark as Category - waiting to be replied to.

Script searchs only Example Enquiry Folder for Waiting To be Replied To and after processing either
a) Flags them as missing info (to be determined by script)
b) Marks them as replied to to avoid extra processing by script

What we need now to add is the ability to grab the information that we need out of the email (firstname etc) and the auto sending of the reply (unless I missed that in your script?).

Cheers Sam,

Dean
Dean,
    In reading over your goals for this reply more carefully I'm inclined to think I may not be the man for your project (at least not the way you described it). I'm not a web designer, so sending someone a HTML questionnaire is seeming to be a bit out of my league (I looked into it a little). That does not mean it is impossible of course, but I am more of a VBA guy and I'm a bit lost when it comes to HTML.
    I'm more than happy to help you parse the text out of the message to get the information needed and my code above handles the criteria and generating a reply, but you may need someone else to help you create a HTML questionnaire.

    An alternative (if I may suggest one) could be to use an excel spreadsheet as your questionaire, then when the script runs it pulls the data from the email, generates a reply with just a text message in the body asking them to please fill out the attached Excel sheet and return it.

Let me know how you want to proceed, OK?

God bless!

Sam
Sam,

I already have the HTML for the questionnaire but I just currently have to send it manually to each email that arrives. I just want to automate the whole process so I wouldn't fret over it at all since its the VBA bit that I need help with :)

Though your suggestion of attachments, if we can implement it, looks very interesting however I am unsure how many of our clients will have a copy of Excel. They aren't the most techno-savvy demographic of the population. But I presume that the attachment could be almost anything?

Thanks you for your time and God bless you too.

Dean
Dean,
    If you already have the HTML I will look at figuring out how to get the data from the email, add it to the contents of the HTML and then insert the whole thing into the message body of the reply. I'll try to get back with more details soon.

Sam
Sam,

Thanks for that - I look forward to hearing back from you - don't work too hard over the weekend!!

Dean
Sam,

I just noticed from your earlier reply, that you are looking at adding the data from the email _to the contents of the HTML_. I'm not sure that is strictly neccessary - We can send the email in plain text, with an HTML attachment.

Hope that helps.

Dean
Hi Dean,
    I was glad to hear that you can send the HTML as an attachment. That does make the process easier. Here is the next step (I think). This will now scan the directory and pick out the ones needing a reply, pull out the Title, First & Last name, set the case for these three to ProperCase and insert them into the top of the reply message.
'*******************************
Sub CustReply()

    Dim oOutlook As Outlook.Application
    Dim oNs As Outlook.NameSpace
    Dim oFldr As Outlook.MAPIFolder
    Dim oMessage As Object
    Dim oReply As Object
    Dim strBody As String
    Dim strCriteria As String
    Dim strReply As String
    Dim strTitle As String, strFirstNm As String, strLastNm As String

    'Set and pick the Outlook folder to look through
    Set oOutlook = New Outlook.Application
    Set oNs = oOutlook.GetNamespace("MAPI")
    Set oFldr = oNs.PickFolder
    If oFldr Is Nothing Then
        GoTo ReplyCleanUp
    End If
   
    'Create criteria string variable
    strCriteria = "========================" & vbCrLf
    strCriteria = strCriteria & "Example Enquiry" & vbCrLf
    strCriteria = strCriteria & "========================"
   
    'Scan through each mail item in the selected Outlook folder
    For Each oMessage In oFldr.Items
        'First check if Category has been marked for e-mails already proccessed
        If oMessage.Categories <> "CstRep" Then
            'If message body contains criteria string set above
            If oMessage.Body Like "*" & strCriteria & "*" Then
                Set oReply = oMessage.Reply
               
                'Parse incoming e-mail text body
                strTitle = Trim(Mid(oMessage.Body, InStr(1, oMessage.Body, "Title: ") _
                            + Len("Title: "), InStr(InStr(1, oMessage.Body, "Title: ") _
                            + Len("Title: "), oMessage.Body, vbLf) - (InStr(1, oMessage.Body, _
                            "Title: ") + Len("Title: ")) - 1))
                strFirstNm = Trim(Mid(oMessage.Body, InStr(1, oMessage.Body, "First Name: ") _
                            + Len("First Name: "), InStr(InStr(1, oMessage.Body, "First Name: ") _
                            + Len("First Name: "), oMessage.Body, vbLf) - (InStr(1, oMessage.Body, _
                            "First Name: ") + Len("First Name: ")) - 1))
                strLastNm = Trim(Mid(oMessage.Body, InStr(1, oMessage.Body, "Last Name: ") _
                            + Len("Last Name: "), InStr(InStr(1, oMessage.Body, "Last Name: ") _
                            + Len("Last Name: "), oMessage.Body, vbLf) - (InStr(1, oMessage.Body, _
                            "Last Name: ") + Len("Last Name: ")) - 1))
               
                'Set case for string variables
                strTitle = StrConv(strTitle, vbProperCase)
                strFirstNm = StrConv(strFirstNm, vbProperCase)
                strLastNm = StrConv(strLastNm, vbProperCase)
               
                'Create string to insert into the top of the reply message
                strReply = "To: " & IIf(Not IsNull(strTitle), strTitle & ", ", " ") & strFirstNm & " " & strLastNm

                With oReply
                    .Body = strReply & vbCrLf & vbCrLf & .Body
                    .Display
                End With
                oMessage.Categories = "CstRep"
                oMessage.Save
            End If
        End If
    Next oMessage

ReplyCleanUp:

    Set oOutlook = Nothing
    Set oNs = Nothing
    Set oFldr = Nothing
    Set oMessage = Nothing
   
End Sub
'*******************************
Let me know how it works for you and we can go on to adding your attachments.

God bless!

Sam.
Hey Sam,

Good to hear back from you - hope you had a good weekend.

Thank you very much for your script. Unfortunately, I get an error when I run it. Let me tell you what I am doing and you can tell me where I am going wrong :)

I create a Macro called CustReply
I run the Macro
I select a folder (in this case, called test with one message in which looks like the one in my first post)
I get a To: ==================, ============= ==============

Is this something I am doing wrong.

I also have one other question - is it possible to preset the folder and have it run when automatically when all the rules have finished processing?

Thanks again Sam

Dean
Hi Dean
    It does not look like something you are doing wrong. I actually had the same problem, but I thought it was the formatting of the specific email as the others I was testing it with worked fine. I will look into it and try to find out what it's doing wrong.

Get back to you soon.

Sam.
I found out what was wrong. The code above looks for strings where the colon is imdiately after the word (for example "Title:" instead of "Title :"). My bad as I must have read and pasted it wrong in my test emails. If you paste this section over the old one it should work properly:

                'Parse incoming e-mail text body
                strTitle = Trim(Mid(oMessage.Body, InStr(1, oMessage.Body, "Title : ") _
                            + Len("Title : "), InStr(InStr(1, oMessage.Body, "Title : ") _
                            + Len("Title : "), oMessage.Body, vbLf) - (InStr(1, oMessage.Body, _
                            "Title : ") + Len("Title : ")) - 1))
                strFirstNm = Trim(Mid(oMessage.Body, InStr(1, oMessage.Body, "First Name : ") _
                            + Len("First Name : "), InStr(InStr(1, oMessage.Body, "First Name : ") _
                            + Len("First Name : "), oMessage.Body, vbLf) - (InStr(1, oMessage.Body, _
                            "First Name : ") + Len("First Name : ")) - 1))
                strLastNm = Trim(Mid(oMessage.Body, InStr(1, oMessage.Body, "Last Name : ") _
                            + Len("Last Name : "), InStr(InStr(1, oMessage.Body, "Last Name : ") _
                            + Len("Last Name : "), oMessage.Body, vbLf) - (InStr(1, oMessage.Body, _
                            "Last Name : ") + Len("Last Name : ")) - 1))
               
Thanks Sam,

I'll check it out in the morning.

Have a nice evening
Dean
Hey Sam,

That seems to work really well - thank you very much.

Now we just need to get the attachment working and figure out a way to run the macro automagically each time a send and receive happens ;)

Thanks again
Dean
Hi Dean,
    Sorry I have not been back to this for a day or so. Work has gotten quite busy. You should be able to add your attachment by adding a single line inside the "with oReply" block like so:

                With oReply
                    .Body = strReply & vbCrLf & vbCrLf & .Body
                    .Attachments.Add("Full Path & Name of your attachment")
                    .Display
                End With

God bless!
Thanks Sam,

I'm away from the office for a few days so I will check it out over the weekend. If you come up with a way of 'automagically' running it each send recevie that would be great.

I hope you have a great weekend and I will 'speak' to you on Monday.

Have fun and thanks again
Dean
Hope you have an enjoyable weekend!
God bless!
Sam
Sorry for the delay in getting back to you Sam,

That works a treat (the attachment bit!). OK whats left on the wish list?

1. Automagical running on each send and receive - perhaps using the MAPIFolder.Items.ItemAdd event?
2. Setting the name of the folder so it doesn't have to be chosen each time its run

and

3. Not on the original list so not so important - preventing the security dialog box popping up each time asking you for permission for access to the emails.

Sam, you are a gentleman and a scholar and I appreciate all your help
Dean
Hi Dean,
    I'm in the middle of a project right now so I have not had time to look into this much more. I posted another question to try and solicit some help on the first thing you listed above and I believe it can be solved with the advice I've recieved, but like I said I have not had much time so I hanve not yet put it into practice. If you want to look at it and see if you can get it working it is here:
    https://www.experts-exchange.com/questions/21110172/Autorun-MACRO-on-Send-Recieve.html
If you have any questions about the above method you might want to post them here rather than in my question, just so that the expert does not get the impression that you are trying to get a free answer since I have not yet closed the question.

I'll try to get back to you on all of this soon.

God bless!

Sam
Sam,

I have absolutely no interest in trying to get a free answer!!! I do have questions about your other thread but mainly they revolve around 'how does that work?' ;)

All credit is due to you for the work that you have put in and I would never consider trying to 'rob' you of your points.

Since the level is beyond me now, I will leave it to you to integrate the two pieces of code and happily award you all the points when done. I may try messing around with the code but if I get it to work it will be not through any understanding of the code :) I will still prefer to wait for you to give me a solid, reliable solution rather than the jerry-rigged solution I will end up with.

Good luck on your project and I await your reply

Dean

Sorry Dean,
    I don't think I was clear. I'm not worried about you trying to get a free answer, I was just concerened that stefri (the expert who was helping me in that thread) might get the wrong impression if someone other than I asked questions about his solution since I am the one he is answering and he does not know that the reason I asked the question is for your situation (so he might think he is giving 2 answers for the price of one). I've done it to other experts unwitingly and have seen a few  get a little bothered at me for it.

I will gladly get you what help I can as soon as possible (I also don't mind if someone else offers a solution that brings this farther along).

God bless!
Sam

Ahh. Sorry - my mistake. Carry on folks - nothing to see here!

:blush:

Dean
Any further progress Sam?

Cheers
Dean
Hi Dean,
I'll try to get something posted on this today or tomorrow. Sorry it is taking so long.

God bless!

Sam
No worries Sam - just checking you were still there :)

Dean
ASKER CERTIFIED SOLUTION
Avatar of will_scarlet7
will_scarlet7

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
PS. I think doing the MACRO this way will solve both the issue of setting the folder to process automatically in the MACRO, and the security popups as running it in "ThisOutlookSession" seems to get around the security blocks (at least it did on my machine).
PPS. One caution about the above code. It was not designed to catch sending mail to yourself so I would recommend using either another Outlook profile or an outside mail service such as yahoo or hotmail  for sending any test messages, otherwise you could end up with a bit of a circle as the app will keep sending the auto reply each time one of them comes in.
Sam,

I'm just rebuilding my system o it will be a few days before I can test this. Please accept my apologies for the delay

Dean
No worries,
God bless!
Hey Sam,

So sorry for the delay ..... blinking PC problems..... grrr. Just finishing off the re-install today (finally!) and will be looking at this over the weekend. My apologies for taking so long to do this.

Dean
No worries. Don't work to hard this weekend.
God bless!
Hey Sam,

Maybe its something I did during the re-install but this doesn't appear to do anything on my system. Using your original macro - waay up high on the page here :) it works fine but following :

To add this MACRO:
1.) On the Tools menu, point to Macro, and click Visual Basic Editor.
2.) In the Project - Project1 pane, double-click Project1, and double-click Microsoft Outlook Objects.
3.) Double-click ThisOutlookSession to open a code window.
4.) In the code window, paste the following code.
'***********************************
 And then the script - does nothing here. Nothing at all. Maybe I'm doing something wrong but I can't see it :(
Are you testing it with mail already downloaded or by sending new messages?
Have you re-started Outlook since you pasted it in?

This MACRO needs to be in place when Outlook starts, and then only monitors for new incoming messages.
Hi Sam,

This works great - it was my bonehead error. After the re-install I still hadn't lowered the Macro security level. Doh!

Thank you so much
Dean
That's great to hear (that it worked for you)! Glad I could be some help.

God bless!
Sam