Excel Macro for automated emails through outlook

gavstock89

New Member
Joined
Feb 7, 2023
Messages
7
Office Version
  1. 365
  2. 2021
Platform
  1. Windows
I have a spreadsheet whereby i want to send an automated email every time I open the file based on a set criteria.

Criteria being -

If column D = "specific text" then send email to column G = "outlook email address" (where the cells in column D and G are in the same row)
Also, once email has been sent, some sort of check so that we don't keep sending the same email every time we reopen the file
I understand I can do this with "hyperlink" formula, but want it automated as soon as spreadsheet is opened.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet. Alternately, you could upload a copy of your file de-sensitized if necessary) to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. You haven't stated the subject and body of the the emails. Also, we will need an empty column to keep track of which emails have been sent. What is the first blank column in your sheet?
 
Upvote 0
It would be easier to help if you could use the XL2BB add-in (icon in the menu) to attach a screenshot (not a picture) of your sheet. Alternately, you could upload a copy of your file de-sensitized if necessary) to a free site such as www.box.com or www.dropbox.com. Once you do that, mark it for 'Sharing' and you will be given a link to the file that you can post here. You haven't stated the subject and body of the the emails. Also, we will need an empty column to keep track of which emails have been sent. What is the first blank column in your sheet?
lets use column J to track "if sent"

so if column D = "out of date" send email to column G (in this case ben@abc.com) and mark column J once done.

Agreement Ref.TypeAgreementStatusCompanyScope of AgreementEmailEmailDate NDA raisedEmail sent?
CA / EXT / 2446ExternalCA - ExternalLiveMURALorder to evaluate the potential for, or the expansion of, a business relationship between
the parties
gavin@abc.comGavin08/11/2021
CA / Int / 2447InternalCA - StandardOut of dateConsultingdiscussion and evaluationben@abc.comBen09/11/2021
 
Upvote 0
Place this macro in the code module for ThisWorkbook. Do the following: Hold down the ALT key and press the F11 key. This will open the Visual Basic Editor. In the left hand pane, double click on "ThisWorkbook". Copy/paste the macro into the empty window that opens up. Close the window to return to your sheet. Save the workbook as a macro-enabled file. Close the file and re-open it to run the macro.
VBA Code:
Private Sub Workbook_Open()
    Application.ScreenUpdating = False
    Dim OutApp As Object, OutMail As Object, srcRng  As Range, status As Range
    Set OutApp = CreateObject("Outlook.Application")
    With ActiveSheet
        .Range("A1").CurrentRegion.AutoFilter 4, "Out of date"
        Set srcRng = .Range("D2", .Range("D" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
        For Each status In srcRng
            If .Range("J" & status.Row) <> "Yes" Then
                Set OutMail = OutApp.CreateItem(0)
                With OutMail
                    .To = status.Offset(, 3).Value
                    .Subject = ""
                    .HTMLBody = ""
                    .Display
                End With
            End If
            .Range("J" & status.Row) = "Yes"
        Next status
        .Range("A1").AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Place this macro in the code module for ThisWorkbook. Do the following: Hold down the ALT key and press the F11 key. This will open the Visual Basic Editor. In the left hand pane, double click on "ThisWorkbook". Copy/paste the macro into the empty window that opens up. Close the window to return to your sheet. Save the workbook as a macro-enabled file. Close the file and re-open it to run the macro.
VBA Code:
Private Sub Workbook_Open()
    Application.ScreenUpdating = False
    Dim OutApp As Object, OutMail As Object, srcRng  As Range, status As Range
    Set OutApp = CreateObject("Outlook.Application")
    With ActiveSheet
        .Range("A1").CurrentRegion.AutoFilter 4, "Out of date"
        Set srcRng = .Range("D2", .Range("D" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible)
        For Each status In srcRng
            If .Range("J" & status.Row) <> "Yes" Then
                Set OutMail = OutApp.CreateItem(0)
                With OutMail
                    .To = status.Offset(, 3).Value
                    .Subject = ""
                    .HTMLBody = ""
                    .Display
                End With
            End If
            .Range("J" & status.Row) = "Yes"
        Next status
        .Range("A1").AutoFilter
    End With
    Application.ScreenUpdating = True
End Sub
thanks mumps, if possible, could you explain each line, so if i need to amend in the future (more columns / data etc) i will know how to?

also, i'm guessing when you send it the sending will show as whoever has opened the file? isit possible to send the automated email from a different email address (instead of individual, it could show as coming from the department for example)
 
Upvote 0
thanks mumps, if possible, could you explain each line, so if i need to amend in the future (more columns / data etc) i will know how to?

also, i'm guessing when you send it the sending will show as whoever has opened the file? isit possible to send the automated email from a different email address (instead of individual, it could show as coming from the department for example)
also, when running trying to send the email i am getting two outlook boxes popping up.

1st showing the recipients address and 2nd just showing "blank"?
 
Upvote 0
When you send the email, it will display the email address of the person/department who is attached to the outlook account sending the email. I don' think that can be changed.
when running trying to send the email i am getting two outlook boxes popping up.

1st showing the recipients address and 2nd just showing "blank"?
I tested the macro and it worked properly. I'm not sure what you mean by "outlook boxes".
Here is the code with some explanatory comments:
VBA Code:
Private Sub Workbook_Open()
    Application.ScreenUpdating = False
    Dim OutApp As Object, OutMail As Object, srcRng  As Range, status As Range
    Set OutApp = CreateObject("Outlook.Application")
    With ActiveSheet
        .Range("A1").CurrentRegion.AutoFilter 4, "Out of date" 'filters the data based on 'Out of date' in column 4
        Set srcRng = .Range("D2", .Range("D" & Rows.Count).End(xlUp)).SpecialCells(xlCellTypeVisible) 'sets the srcRng to the visible cells in column D
        For Each status In srcRng 'loops through the 'Email Sent?' column
            If .Range("J" & status.Row) <> "Yes" Then 'checks to see if it has been sent
                Set OutMail = OutApp.CreateItem(0)
                With OutMail
                    .To = status.Offset(, 3).Value
                    .Subject = ""
                    .HTMLBody = ""
                    .Display
                End With
            End If
            .Range("J" & status.Row) = "Yes" 'once sent, it marks it as 'Yes"
        Next status
        .Range("A1").AutoFilter 'removes the autofilter
    End With
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thanks Mumps, another quick thing.

Is it possible, in the HTMLBody field, to reference a particular cell? For example, the macro finds the "out of date" fields and sends an email to the corresponding address, when the email sends it references column a for example?
 
Upvote 0
when the email sends it references column a for example?
Please explain in more detail using an example from your data referring to specific cells, rows and columns.
 
Upvote 0
So when sending the automated email, i would like it to reference a particular cell (in the same row as the searched field) and return that value?

So from my data set column D = "out of date" send email to column G (in this case ben@abc.com) and mark column J once done, but when sending the email reference column A
 
Upvote 0

Forum statistics

Threads
1,214,979
Messages
6,122,551
Members
449,088
Latest member
davidcom

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