Consulting

Page 1 of 3 1 2 3 LastLast
Results 1 to 20 of 41

Thread: Macro to send out email based on criteria via outlook

  1. #1
    VBAX Regular
    Joined
    Apr 2015
    Posts
    18
    Location

    Macro to send out email based on criteria via outlook

    Good morning,

    I'm not entirely sure if what I am after is possible however hopefully some sort of solution will be possible!

    I am looking for a macro that can be run within an open email in outlook that will look for specific criteria and then email a specific recipient based on that criteria after matching it against information within an excel sheet.

    The macro would look for the information from within an excel spreadsheet and match that against the information contained within the email and in turn send out a notification to specified recipients.

    The criteria would be - Subject, String within attachment name and sender email.

    I have attached an example of what the spreadsheet would look like, it contains the fields File Name Subject Sender Email Notification

    Excel forum.xlsx

    So for example if an email came in and had the necessary criteria that matched that line in the excel sheet, an email would then be sent out to the email address/addresses mentioned in the "Email Notification" column by pressing this macro.

    The emails that would get sent out as notifications would all contain the same message, for testing purposes something like "Notification of email arrival" would do.

    If you have any questions please let me know, thanks in advance for any help as it is greatly appreciated.

    Thanks,

    Matt

  2. #2
    What you ask is reasonably straightforward given the type of workbook you envisage. The macro is probably best run as a script from a rule to check the messages as they arrive, but you can run the TestMsg macro to both test and process individual messages. Change the path and worksheet name as appropriate. Select a message and run TestMsg.
    The macro reads the named worksheet into an array. This is very fast in practice, as is the search. The values are then compared with the subject, sender and attachment. Anything that meets the criteria results in a raised message.

    Option Explicit
    Const strWorkbook As String = "C:\Path\Excel forum.xlsx"        'The path of the workbook
    Const strSheet As String = "Sheet1"        'The name of the worksheet
    
    Sub TestMsg()
    Dim olMsg As MailItem
        On Error Resume Next
        Set olMsg = ActiveExplorer.Selection.Item(1)
        AutoReply olMsg
    lbl_Exit:
        Exit Sub
    End Sub
    
    Sub AutoReply(olMail As Outlook.MailItem)
    Dim olReply As Outlook.MailItem
    Dim olInsp As Outlook.Inspector
    Dim wdDoc As Object
    Dim oRng As Object
    Dim Arr() As Variant
    Dim iCols As Long
    Dim iRows As Long
    Dim strName As String
    Dim oAtt As Attachment
        'load the worksheet into an array
        Arr = xlFillArray(strWorkbook, strSheet)
        With olMail
            For iRows = 0 To UBound(Arr, 2)        'Check each row of the array
                'If column 2 (starting at column 0) contains the e-mail address of the message
                If .SenderEmailAddress = Arr(2, iRows) Then
                    'If the subject value is in the message subject
                    If InStr(1, .Subject, Arr(1, iRows)) > 0 Then
                        'process the attachments to the message
                        For Each oAtt In .Attachments
                            'If any attachment filename has the text in column 0
                            If InStr(1, oAtt.Filename, Arr(0, iRows)) > 0 Then
                                'Create a message
                                Set olReply = CreateItem(olMailItem)
                                With olReply
                                    .Subject = Arr(1, iRows)
                                    .To = Arr(3, iRows)
                                    .BodyFormat = olFormatHTML
                                    .Display
                                    Set olInsp = .GetInspector
                                    Set wdDoc = olInsp.WordEditor
                                    Set oRng = wdDoc.Range(0, 0)
                                    oRng.Text = "Notification of email arrival"
                                    '.sEnd 'Restore after testing
                                End With
                                Exit For
                            End If
                        Next oAtt
                    End If
                End If
            Next iRows
        End With
    lbl_Exit:
        Set olReply = Nothing
        Set olInsp = Nothing
        Set wdDoc = Nothing
        Set oRng = Nothing
        Exit Sub
    End Sub
    
    Private Function xlFillArray(strWorkbook As String, _
                                 strWorksheetName As String) As Variant
    Dim RS As Object
    Dim CN As Object
    Dim iRows As Long
    
        strWorksheetName = strWorksheetName & "$]"
        Set CN = CreateObject("ADODB.Connection")
        CN.Open ConnectionString:="Provider=Microsoft.ACE.OLEDB.12.0;" & _
                                  "Data Source=" & strWorkbook & ";" & _
                                  "Extended Properties=""Excel 12.0 Xml;HDR=YES"";"
    
        Set RS = CreateObject("ADODB.Recordset")
        RS.Open "SELECT * FROM [" & strWorksheetName, CN, 2, 1
    
        With RS
            .MoveLast
            iRows = .RecordCount
            .MoveFirst
        End With
        xlFillArray = RS.GetRows(iRows)
        If RS.State = 1 Then RS.Close
        Set RS = Nothing
        If CN.State = 1 Then CN.Close
        Set CN = Nothing
    lbl_Exit:
        Exit Function
    End Function
    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
    VBAX Regular
    Joined
    Apr 2015
    Posts
    18
    Location
    Thanks very much for the reply. Unfortunately this doesn't seem to be working, I am pressing the macro but getting no response.

    What I have been doing is opening an email up then pressing the macro, is this how it should be ran? Apologies for the questions however my knowledge is very low. Below are my column headers, do these need changing for example?

    File Name Subject Sender Email Notification
    Last edited by MD011; 04-09-2015 at 07:43 AM.

  4. #4
    The macro won't do anything if the message is not from the sender's e-mail address in the Excel list, OR the subject text doesn't contain the text (case sensitive) in the matching worksheet record, OR the attachment name (case sensitive) doesn't contain the text in the worksheet record. All three have to match for the macro to produce a response. Create a record in your data sheet that has the three items in the message you are testing and then test it.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  5. #5
    VBAX Regular
    Joined
    Apr 2015
    Posts
    18
    Location
    Thanks again for the help it is much appreciated.

    Is there any way to change the code so that it only looks for a specific string in the mentioned columns? So for example if an email came through with the subject "testing help" then i would just put "testing" in my column named "subject" instead of the full subject if that makes sense?

    Or can i do that already by simply changing my entries in my excel sheet?

  6. #6
    You don't need to change anything to achieve that, as the macro already looks for the column text in the subject

    If InStr(1, .Subject, Arr(1, iRows)) > 0 Then
    Arr(1, iRows) is the data from the Subject column of the worksheet. If you have 'testing' in The column, it will match with 'testing help' in the message subject. It will not however match with 'Testing help'. If the subject may have mixed case, change the line to

    If InStr(1, LCase(.Subject), Arr(1, iRows)) > 0 Then
    and ensure the worksheet column only has lower case entries.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  7. #7
    VBAX Regular
    Joined
    Apr 2015
    Posts
    18
    Location
    Hi again, i unfortunately still have not been able to get this to function correctly.

    Any further help will be appreciated as I am keen to get this up and running.

  8. #8
    What happens when you try? In what way is it not correct?
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  9. #9
    VBAX Regular
    Joined
    Apr 2015
    Posts
    18
    Location
    Nothing is happening, that is the problem, I can only imagine that perhaps something isn't setup correctly. I am ensuring that the File name, subject, sender are being met and have a valid email address in the email notification column.

    I am then opening the email and running the macro manually. What could be causing this? Am i doing anything incorrect?

    For example I sent myself an email with the following details:

    Where I have put "" this is where i have entered the correct email adress.

    File name - Testing.xlsx
    Subject - test
    Sender - ""

    I then filled in a row in the sheet with the following info:

    File name - Testing
    Subject - test
    Sender - ""
    Email Notification - ""

    Any help would be greatly appreciated.

  10. #10
    You can't use nul values in the worksheet. If there is no sender and no e-mail notification address the criteria are never met.
    You need three pieces of information in the message to match and thus create a message to the address in email notification.
    I changed the sender address in your original worksheet and it works as intended.

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

  11. #11
    VBAX Regular
    Joined
    Apr 2015
    Posts
    18
    Location
    Thanks again, the null values i entered "" were actually email addresses, it just wouldn't let me type the address into my message on the forum for some reason!

    So in fact I am actually entering full email addresses however nothing is happening. The only thing I can think of is that the filepath may be wrong?

  12. #12
    The following must correctly match what you have:

    Const strWorkbook As String = "C:\Path\Excel forum.xlsx" 'The path of the workbook
    Const strSheet As String = "Sheet1" 'The name of the worksheet
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  13. #13
    VBAX Regular
    Joined
    Apr 2015
    Posts
    18
    Location
    Indeed i was under the impression that I had matched them correctly. I will triple check. Once again I appreciate the help.

  14. #14
    The bottom line is that the message you are processing must have the three items shown in the above illustration listed in the same row of your excel data sheet.
    I would have expected an error message if the workbook/sheet data was wrong.
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  15. #15
    VBAX Regular
    Joined
    Apr 2015
    Posts
    18
    Location
    I'm pretty sure my criteria do match, i have tried multiple entries and switching things around but still no luck. I will keep trying but I am very confused! Is there any way I could be running it correctly? Also i have tried just putting a string from the email address in, is this acceptable?

  16. #16
    I really don't know what more I can tell you. Add a message box where shown below

    With olMail
            For iRows = 0 To UBound(Arr, 2)        'Check each row of the array
                'If column 2 (starting at column 0) contains the e-mail address of the message
                'Add the following message box here
                MsgBox .SenderEmailAddress & vbCr & Arr(2, iRows) & vbCr & vbCr & _
                       .Subject & vbCr & Arr(1, iRows) & vbCr & vbCr & _
                       Arr(0, iRows)
    Select a message and run the TestMsg macro. Using your original worksheet, you should see something like

    If the top pair are the same (unlike here); the word from the second line of the second pair is in the first line of the second pair and you have an attachment name with the word from last line, then the macro should create a message.
    Attached Images Attached Images
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  17. #17
    VBAX Regular
    Joined
    Apr 2015
    Posts
    18
    Location
    It is working now, thank you very much for the help, greatly appreciated.

  18. #18
    VBAX Regular
    Joined
    Apr 2015
    Posts
    18
    Location
    Would it be possible to make the following small tweaks:

    1. The messages send automatically so i dont need to manually click send.
    2. The text inside the email states the Attachment, the sender address and the subject?

    It would be perfect if that is possible

  19. #19
    If you remove the apostrophe from start of the line

    '.sEnd 'Restore after testing
    it will send the messages automatically. I left that in for ease of testing.

    The message body text is defined at the following line.

    oRng.Text = "Notification of email arrival"
    You can change the value to reflect any message you require e.g

    oRng.Text = "Notification of email arrival" & vbCr & vbCr & _
                                    "Sender: " & .Sender & vbCr & _
                                    "Subject: " & .Subject & vbCr & _
                                    "Attachment: " & oAtt.FileName
    Graham Mayor - MS MVP (Word) 2002-2019
    Visit my web site for more programming tips and ready made processes
    http://www.gmayor.com

  20. #20
    VBAX Regular
    Joined
    Apr 2015
    Posts
    18
    Location

    Unhappy

    Thank you very much. Unfortunately the below code does not seem to be working? The macro will no longer bring up an email:

    oRng.Text = "Notification of email arrival" & vbCr & vbCr & _
    "Sender: " & .Sender & vbCr & _
    "Subject: " & .Subject & vbCr & _
    "Attachment: " & oAtt.FileName

    I simply replaced the original code - oRng.Text = "Notification of email arrival"

    Is that how it should be done? Apologies for the basic questions here however my knowledge is very slim!

    It now seems to be bringing the emails up HOWEVER it will not include the sender, subject or attachment info in the message.
    Last edited by MD011; 05-18-2015 at 04:42 AM.

Posting Permissions

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