Schedule email from list

theboyscout

New Member
Joined
Nov 29, 2012
Messages
26
Hello,

I'm trying to create a script that would search a folder, find the last inserted spreadsheet, locate a field and if that field is > 0 send an email. And this is to be run weekly.

[TABLE="width: 500"]
<tbody>[TR]
[TD="align: center"]F[/TD]
[TD="align: center"]L[/TD]
[TD="align: center"]O[/TD]
[/TR]
[TR]
[TD="align: center"]Descr[/TD]
[TD="align: center"]Email[/TD]
[TD="align: center"]Available_Spots[/TD]
[/TR]
[TR]
[TD="align: center"]60749[/TD]
[TD="align: center"]jon.snow@got.com[/TD]
[TD="align: center"]1[/TD]
[/TR]
</tbody>[/TABLE]


I've scraped up a number of scripts from other users (unfortunately, I can't remember profile names to give them credit), see
below
:

Sub Waitlist_Email()

Dim MyPath As String
Dim MyFile As String
Dim LatestFile As String
Dim LatestDate As Date
Dim LMD As Date
Dim OutApp As Object
Dim OutMail As Object
Dim strbody, SigString, Signature As String
Dim MailAttachments As String
Dim cell As Variant
Dim GetBoiler As Object

'Search for recent file in folder
MyPath = “C:\Users\Desktop\Waitlist"
If Right(MyPath, 1) <> “ \ ” Then MyPath = MyPath & “ \ ”
MyFile = Dir(MyPath & “ * .xls”, vbNormal)
If Len(MyFile) = 0 Then
MsgBox “No files were found…”, vbExclamation
Exit Sub
End If
Do While Len(MyFile) > 0
LMD = FileDateTime(MyPath & MyFile)
If LMD > LatestDate Then
LatestFile = MyFile
LatestDate = LMD
End If
MyFile = Dir
Loop
Workbooks.Open MyPath & LatestFile

'If file is located send email
Sheets("Sheet1").Select
Range("A1").Select

Application.ScreenUpdating = False
Set OutApp = CreateObject("Outlook.Application")

'On Error GoTo cleanup
For Each cell In Columns("L").Cells
If cell.Value Like "?*@?*.?*" And _
LCase(Cells(cell.Row, "O").Value) > "0" Then

With Application.ActiveSheet
MailAttachments = Cells(cell.Row, "E").Value
End With


Set OutMail = OutApp.CreateItem(0)

On Error Resume Next

With OutMail

.To = cell.Value
.Subject = "Enrollment available " & Cells(cell.Row, "F").Value 'Refer value from column F (Course Description)
.HTMLBody = "" & _
"Hello ," & Cells(cell.Row, "H") & ", " & ""
" & _
"Blah blah blah " & Cells(cell.Row, "F") & " blah blah blah " & Cells(cell.Row, "K") & " blah blah blah. " & ""
" & _
"
" & _
"
" & _
"
" & _
"**************************************************************************************************************" & "
" & _
"This is your signature file. It will always be 7 rows below whatever text you add in the macro code."
'To add another line in your message or signature without skipping a row, end the line of text with
'a quotation symbol and then an ampersand and an underscore. Ex: end of text." & _
'.Attachments.Add MailAttachments
.Display
'Or use .Send

End With

On Error GoTo 0
End If
Next
cleanup:
Set OutMail = Nothing
Set OutApp = Nothing
Application.ScreenUpdating = True

End Sub

I'm getting error message at
" & _
<strike></strike> and I can't figure out how to schedule to run weekly.

Any help would be appreciated
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
.
Once the macro has opened the newest file, what sheet and cell are you looking at for a value ?

And, what value are you expecting in that cell, which will initiate the email sending ?
 
Upvote 0
It'll be Sheet1 and the cell I'll be looking at for a value is O. For each instance that in cell O there is a value greater than 0 then send an email
 
Upvote 0
.
This is part of the code :

Code:
Option Explicit


Sub send_email_via_outlook()


' Tools - Refrence - Microsoft Outlook
Dim olApp As New Outlook.Application
Dim olMail As MailItem


Set olMail = olApp.CreateItem(olMailItem)
    
    With olMail
        .To = "test@gmail.com"
        .CC = ""
        .Subject = "Send Range as table in outlook"  '<br> used to insert a line ( press enter)
        .HTMLBody = "Please find the table below <br><br> " & _
                    create_table(Sheet2.Range("A1").CurrentRegion) & _
                    "</Table><br> <br>Regards,<br><br>Afred E. Newman"
        .Display
        '.Send
    End With




End Sub


Function create_table(rng As Range) As String


Dim mbody As String
Dim mbody1  As String
Dim i As Long
Dim j As Long


mbody = "<TABLE width=""75%"" Border=""1"", Cellspacing=""0""><TR>" ' configure the table


'create Header row
For i = 1 To rng.Columns.Count
    mbody = mbody & "<TD width=""100"", Bgcolor=""#A52A2A"", Align=""Center""><Font Color=#FFFFFF><b><p style=""font-size:18px"">" & rng.Cells(1, i).Value & " </p></Font></TD>"
Next


' add data to the table
For i = 2 To rng.Rows.Count
    mbody = mbody & "<TR>"
    mbody1 = ""
    For j = 1 To rng.Columns.Count
    mbody1 = mbody1 & "<TD><center>" & rng.Cells(i, j).Value & "</TD>"
    Next
    mbody = mbody & mbody1 & "</TR>"
Next


create_table = mbody
End Function


Examine the download to view the remainder of the code.

Download workbook : https://www.amazon.com/clouddrive/share/2lC7Pk8bPRflJxyOnSkP16urmcqMy4r4DEHsj54m7XP
 
Upvote 0
Hello Logit,

Thanks for your response. Unfortunately, your script doesn't accomplish what I'm looking to do.

I'll elaborate on what I'm trying to get done.

Below is a sample of the spreadsheet that will be generated on a weekly basis. It contains the names and email addresses of people on a waitlist for a course(s). What I'm trying to do is have a script that would run on a schedule and check the last updated spreadsheet in the folder (Waitlist), search the sheet and locate column O. In column O if there is a value greater than 0 I would like an individual email to be sent to each person's email address (column L) notifying them there's a spot available.

[TABLE="class: grid, width: 500"]
[TR]
[TD]A[/TD]
[TD]B[/TD]
[TD]C[/TD]
[TD]D[/TD]
[TD]E[/TD]
[TD]F[/TD]
[TD]G[/TD]
[TD]H[/TD]
[TD]I[/TD]
[TD]J[/TD]
[TD]K[/TD]
[TD]L[/TD]
[TD]M[/TD]
[TD]N[/TD]
[TD]O[/TD]
[TD]P[/TD]
[/TR]
[TR]
[TD]Year[/TD]
[TD]Sess[/TD]
[TD]Cat[/TD]
[TD]Sec[/TD]
[TD]Nbr[/TD]
[TD]Descr[/TD]
[TD]ID[/TD]
[TD]Name[/TD]
[TD]Action[/TD]
[TD]Requestor[/TD]
[TD]Date[/TD]
[TD]Email[/TD]
[TD]Cap[/TD]
[TD]Total[/TD]
[TD]Avail[/TD]
[TD]Position[/TD]
[/TR]
[TR]
[TD]2019[/TD]
[TD][TABLE="width: 56"]
[TR]
[TD]1[/TD]
[/TR]
[/TABLE]
[/TD]
[TD] 738[/TD]
[TD]A[/TD]
[TD]60749[/TD]
[TD]History 1000[/TD]
[TD]124919713[/TD]
[TD]Tora Willett[/TD]
[TD]Enr[/TD]
[TD]
Tora Willett
[/TD]
[TD]11/19/2018 4:35:09 PM[/TD]
[TD]Tora.Willett@hotmail.com[/TD]
[TD]32[/TD]
[TD]32[/TD]
[TD]0[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]
2019
[/TD]
[TD]1[/TD]
[TD] 751[/TD]
[TD]1[/TD]
[TD]60811[/TD]
[TD]Business Policy (BPL) 5100[/TD]
[TD]118275089[/TD]
[TD]Codi Gruner[/TD]
[TD]Enr[/TD]
[TD]
Codi Gruner
[/TD]
[TD]12/14/2018 11:49:50 AM[/TD]
[TD]Codi.Gruner@aol.com[/TD]
[TD]39[/TD]
[TD]38[/TD]
[TD]1[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]
2019
[/TD]
[TD]1[/TD]
[TD]7572[/TD]
[TD]1[/TD]
[TD]60738[/TD]
[TD]Business 9100[/TD]
[TD]189572396[/TD]
[TD]Carmine Paz[/TD]
[TD]Enr[/TD]
[TD]
Carmine Paz
[/TD]
[TD]11/19/2018 4:40:24 PM[/TD]
[TD]Carmine.Paz@gmail.com[/TD]
[TD]25[/TD]
[TD]24[/TD]
[TD]1[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]
2019
[/TD]
[TD]2[/TD]
[TD]704[/TD]
[TD]1[/TD]
[TD]60755[/TD]
[TD]History 1003[/TD]
[TD]186625577[/TD]
[TD]Audry Fentress[/TD]
[TD]Enr[/TD]
[TD]
Audry Fentress
[/TD]
[TD]11/20/2018 4:40:24 PM[/TD]
[TD]Audry.Fentress@hotmail.com[/TD]
[TD]11[/TD]
[TD]8[/TD]
[TD]3[/TD]
[TD]1[/TD]
[/TR]
[TR]
[TD]
2019
[/TD]
[TD]3[/TD]
[TD]705[/TD]
[TD]1[/TD]
[TD]60749[/TD]
[TD]Statistics 1001[/TD]
[TD]228457190[/TD]
[TD]Tu Winborne[/TD]
[TD]Enr[/TD]
[TD]
Tu Winborne
[/TD]
[TD]11/21/2018 4:40:24 PM[/TD]
[TD]Tu.Winborne@gmail.com[/TD]
[TD]32[/TD]
[TD]31[/TD]
[TD]1[/TD]
[TD]1[/TD]
[/TR]
[/TABLE]

Thanks in advance
 
Upvote 0
.
I believe this will accomplish the goal. Note the spelling of folders, workbooks and worksheets in the code. Make certain your names match
those in the code.


Download workbook : https://www.amazon.com/clouddrive/share/dFs5x1jGz9A7z75klHSybYSfILHJj2RmYQE4bwZerVO

Code:
Option Explicit


Sub OpenLatestFile()


    'Declare the variables
    Dim MyPath As String
    Dim MyFile As String
    Dim LatestFile As String
    Dim LatestDate As Date
    Dim LMD As Date
    
    'Specify the path to the folder
    MyPath = Environ("USERPROFILE") & "\Desktop\WaitList\"
    
    'Make sure that the path ends in a backslash
    If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
    
    'Get the first Excel file from the folder
    MyFile = Dir(MyPath & "*.xlsx", vbNormal)
    
    'If no files were found, exit the sub
    If Len(MyFile) = 0 Then
        MsgBox "No files were found...", vbExclamation
        Exit Sub
    End If
    
    'Loop through each Excel file in the folder
    Do While Len(MyFile) > 0
    
        'Assign the date/time of the current file to a variable
        LMD = FileDateTime(MyPath & MyFile)
        
        'If the date/time of the current file is greater than the latest
        'recorded date, assign its filename and date/time to variables
        If LMD > LatestDate Then
            LatestFile = MyFile
            LatestDate = LMD
        End If
        
        'Get the next Excel file from the folder
        MyFile = Dir
   Loop
   
   Dim wb1 As Workbook, wb2 As Workbook, ws1Format As Worksheet, ws2Format As Worksheet


    Application.ScreenUpdating = False
    Set wb1 = ThisWorkbook
    Set wb2 = Workbooks.Open(MyPath & LatestFile)
    Set ws1Format = wb1.Sheets("Sheet2")
    Set ws2Format = wb2.Sheets("Sheet1")


    '' Copy the cells of the "Sheet1" worksheet.
    ws2Format.Cells.Copy wb1.Sheets("Sheet2").Range("A1")


    wb2.Close False 'remove false if you want to be asked if the workbook shall be saved.
    Application.ScreenUpdating = True
   
    eMail
    
    Sheets("Sheet2").UsedRange.ClearContents
    
End Sub


Sub eMail()
Dim lRow As Integer
Dim i As Integer
Dim toDate As Date
Dim toList As String
Dim eSubject As String
Dim eBody As String
Dim OutApp, OutMail As Object
Dim FullIdNo As String
Dim strFilename As String


With Application
    .ScreenUpdating = False
    .EnableEvents = False
    .DisplayAlerts = False
End With


Sheet2.Select
lRow = Cells(Rows.Count, 15).End(xlUp).Row
For i = 2 To lRow
    If Cells(i, 15).Value > 0 Then
         Set OutApp = CreateObject("Outlook.Application")
         Set OutMail = OutApp.CreateItem(0)
    
            toList = Cells(i, 8)   'gets the recipient email address from col X
            eSubject = "Enrollment available : " & Cells(i, 6).Value 'Refer value from column G (Course Description)
            eBody = "Dear " & Cells(i, 8) & " : " & vbCrLf & vbCrLf & "Just a quick note to advise " & Cells(i, 6) & " Class enrollment is available." & vbCrLf & vbCrLf & _
            "Sincerely, " & vbCrLf & vbCrLf & _
            "Your Signature"
            
            On Error Resume Next
            With OutMail
            .To = toList
            .CC = ""
            .BCC = ""
            .Subject = eSubject
            .Body = eBody
            .Display   ' ********* Creates draft emails. Comment this out when you are ready
            '.Send     '********** UN-comment this when you  are ready to go live
            End With
        
    End If
Next i


On Error GoTo 0
        Set OutMail = Nothing
        Set OutApp = Nothing


Application.Goto ActiveWorkbook.Sheets("Sheet1").Range("A1")


With Application
    .ScreenUpdating = True
    .EnableEvents = True
    .DisplayAlerts = True
End With
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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