Ok, I just got past the array problem I was having. The good news is, the code returns no errors when I run it. The bad news is, it does nothing at all when I run it.
I will post the finalized code, but understand for privacy, I had to change client names to "Client" and asset names to "asset". I also changed email addresses for the same reason:
Sub Run(ByVal sFile)
Dim shell
Set shell = CreateObject("WScript.Shell")
shell.Run Chr(34) & sFile & Chr(34), 1, false
Set shell = Nothing
End Sub
'Run "C:\Users\Lee\Documents\Email Templates\client (asset).oft"
const UNKNOWN = "Chickenless Soup"
dim arrAttachments(2)
set objFSO = CreateObject("Scripting.FileSystemObject")
set colFiles = objFSO.GetFolder("C:\Users\Lee\Documents\Morning Reports\client").Files
set dicClients = CreateObject("Scripting.Dictionary")
strFrom = "myaltemail@domain.com"
strCc = "coworkers@domain.com"
strSubject = "Here you go"
strMessage = "Here you go"
function getYesterday()
select case (month(date - 1))
case 1 : strMonth = "Jan"
case 2 : strMonth = "Feb"
case 3 : strMonth = "Mar"
case 4 : strMonth = "Apr"
case 5 : strMonth = "May"
case 6 : strMonth = "Jun"
case 7 : strMonth = "Jul"
case 8 : strMonth = "Aug"
case 9 : strMonth = "Sept"
case 10 : strMonth = "Oct"
case 11 : strMonth = "Nov"
case 12 : strMonth = "Dec"
end select
getYesterdaay = strMonth & "_" & day(date - 1)
end function
function isFileValid(strFileName)
isFileValid = false
'Assuming the files have the same nomeclature of Client_month_day_var.pdf
arrTokens = split(strFileName, "_")
strMonth = arrTokens(1)
strDay = arrTokens(2)
if (getYesterday() = strMonth & "_" & strDay) then
if (right(strFileName, 4) = ".pdf") then
isFileValid = true
end if
end if
end function
for each objFile in colFiles
'Assuming the files have the same nomeclature of Client_month_day_var.pdf
if (isFileValid(objFile.Name)) then
strClient = left(objFile.Name, instr(objFile.Name, "_"))
select case lcase(strClient)
case "asset" : strTo = "manypeople@domain.com"
case else : strTo = UNKNOWN
end select
if (strTo <> UNKNOWN) then
if NOT (dicClients.Exists(strClient)) then
dicClients.Add strClient, array(strTo)
end if
arrAttachments = dicClients.Item(strClient)
intIndex = ubound(arrAttachments) + 1
redim preserve arrAttachment(intIndex)
arrAttachment(intIndex) = objFile.Path
dicClients.Item strClients, arrAttachments
end if
end if
next
arrClients = dicClients.Keys
for i = 0 to ubound(arrClients)
strClient = arrClients(i)
arrAttachments = dicClients.Item(strClient)
next
set objEmail = CreateObject("cdo.message")
objEmail.To = strTo
objEmail.From = strFrom
objEmail.Subject = strSubject
objEmail.Textbody = strMessage
for j = 1 to ubound(arrAttachments)
objEmail.AddAttachment(arrAttachments(j))
next
As you can see, I commented out the line that runs the outlook template that i have created, because it appears that I don't need that with this code. Although, I would prefer to send from that template as it is already set up with the following : To, Cc, Subject, and Body. If I can get the current code to work, I won't worry about the template. If you can see any errors that would make this code do nothing when run, let me know. Thanks
P.S. I appreciate all the help you guys have given me, and if you could hold out for a bit longer I would be very grateful. I believe we are almost there
The only knowledge is knowing that you know nothing.