Need input/confirmation at "@@@@@" and thorough testing
Option Explicit
Option Base 1
Dim pth$
Sub Tasks()
Dim r As Range, cel As Range
Dim ShtsG(), ShtsM()
Dim Arr()
Dim sh As Worksheet
Dim j%, k%, g%, m%
Application.ScreenUpdating = False
On Error Resume Next
MkDir ActiveWorkbook.Path & "\Actions" '@@@@@@@@@@ Creates folder
On Error GoTo 0
pth = ActiveWorkbook.Path & "\Actions\" '@@@@@@@@@@
Call ClearOldFiles
Exit Sub
ReDim ShtsG(1 To Sheets.Count)
ReDim ShtsM(1 To Sheets.Count)
'Get General/Minute sheets
For Each sh In Sheets
If sh.Name Like "Action * General" Then
g = g + 1
ShtsG(g) = sh.Name
End If
If sh.Name Like "Action * Minutes" Then
m = m + 1
ShtsM(m) = sh.Name
End If
Next sh
ReDim Preserve ShtsG(g)
ReDim Preserve ShtsM(m)
'Get Initials
With Sheets("Attendance")
Set r = Range(.Cells(5, 9), .Cells(Rows.Count, 9).End(xlUp))
End With
'Check for initials
ReDim Arr(g + m)
For Each cel In r
'General
For j = 1 To g
If Sheets(ShtsG(j)).Cells(3, 5) = cel Then
k = k + 1
Arr(k) = ShtsG(j)
End If
Next j
'Minutes
For j = 1 To m
If Sheets(ShtsM(j)).Cells(5, 3) = cel Then
k = k + 1
Arr(k) = ShtsM(j)
End If
Next j
'Create action array
If k > 0 Then
ReDim Preserve Arr(k)
Sheets(Arr).Copy
With ActiveWorkbook
.SaveAs pth & cel & ".xlsx", FileFormat:=xlOpenXMLWorkbook
.Close
End With
End If
'Reset array etc.
ReDim Arr(g + m): k = 0
Next cel
Call Emails
Application.ScreenUpdating = True
End Sub
Sub ClearOldFiles()
Dim fNme As String
pth = ActiveWorkbook.Path & "\Actions\"
On Error GoTo Exits
fNme = Dir(pth & "*.xlsx")
Do
Kill pth & fNme
fNme = Dir
Loop Until fNme = ""
Exits:
End Sub
Sub Emails()
Dim fNme$, Init$, Addr$, Att$, Recip$
Dim c As Range
fNme = Dir(pth & "*.xlsx")
Init = Split(fNme, ".")(0)
With Sheets("Attendance")
Do
Set c = .Columns(9).Find(Init, lookat:=xlWhole)
Addr = Split(c.Offset(, -8), " ")(0)
Recip = c.Offset(, 1)
Att = pth & fNme
Call CDO_Mail_Small_Text_2(Addr, Recip, Att)
fNme = Dir
Loop Until fNme = ""
End With
End Sub
'In separate module
Option Explicit
'https://www.rondebruin.nl/win/s1/cdo.htm
'If you have a GMail account then you can try this example to use the GMail smtp server
'The example will send a small text message
'You must change four code lines before you can test the code
'.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "Full GMail mail address"
'.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "GMail password"
'Use your own mail address to test the code in this line
'.To = "Mail address receiver"
'Change YourName to the From name you want to use
'.From = """YourName"" <Reply@something.nl>"
'If you get this error : The transport failed to connect to the server
'then try to change the SMTP port from 25 to 465
'Possible that you must also enable the "Less Secure" option for GMail
'https://www.google.com/settings/security/lesssecureapps
Sub CDO_Mail_Small_Text_2(Addr, Recip, Att)
Dim iMsg As Object
Dim iConf As Object
Dim strbody As String
Dim Flds As Variant
Set iMsg = CreateObject("CDO.Message")
Set iConf = CreateObject("CDO.Configuration")
iConf.Load -1 ' CDO Source Defaults
Set Flds = iConf.Fields
With Flds
.Item("http://schemas.microsoft.com/cdo/configuration/smtpusessl") = True
.Item("http://schemas.microsoft.com/cdo/configuration/smtpauthenticate") = 1
.Item("http://schemas.microsoft.com/cdo/configuration/sendusername") = "SomeOne@gmail.com" '@@@@@@@@@@@
.Item("http://schemas.microsoft.com/cdo/configuration/sendpassword") = "Password" '@@@@@@@@@@@
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserver") = "smtp.gmail.com" '@@@@@@@@@@@
.Item("http://schemas.microsoft.com/cdo/configuration/sendusing") = 2
.Item("http://schemas.microsoft.com/cdo/configuration/smtpserverport") = 25
.Update
End With
strbody = "Hi " & Addr & vbNewLine & vbNewLine & _
"This is line 1" & vbNewLine & _
"This is line 2" & vbNewLine & _
"This is line 3" & vbNewLine & _
"This is line 4" '@@@@@@@@@@@@@@@@@
With iMsg
Set .Configuration = iConf
.To = Recip
.CC = ""
.BCC = ""
' Note: The reply address is not working if you use this Gmail example
' It will use your Gmail address automatic. But you can add this line
' to change the reply address .ReplyTo = "Reply@something.nl"
.From = """YourName"" <Reply@something.nl>" '@@@@@@@@@@@@@@@@@@
.Subject = "Important message" '@@@@@@@@@@@@@@@@@@
.TextBody = strbody
.addAttachment Att
.Send
End With
Set iMsg = Nothing
Set iConf = Nothing
Set Flds = Nothing
End Sub