TekiePrincess
Programmer
I have the following script the works without any errors. It just doesn't send the email. Can someone please give me some advice or guidance of where I went wrong.
sPath = "C:\Documents and Settings\Desktop\TestFolder\"
Set oFSO = CreateObject("Scripting.FileSystemObject")
sNewestFile = GetNewestFile(sPath)
If sNewestFile <> "" Then
WScript.Echo "Newest file is " & sNewestFile
dFileModDate = oFSO.GetFile(sNewestFile).DateLastModified
If DateDiff("h", dFileModDate, Now) > 24 Then
Dim ToAddress (2)
Dim FromAddress
Dim MessageSubject
Dim MessageBody
Dim MessageAttachment
Dim ol, ns, newMail
ToAddress (1) = "xxx@email.com"
ToAddress (2) = "xxx@email.com"
MessageSubject = "Shipment/Movement Files"
MessageBody = "The following Shipment and Movement Transactions files have been received."
MessageAttachment = (sNewestFile)
Set ol = WScript.CreateObject("Outlook.Application")
Set ns = ol.getNamespace("MAPI")
Set newMail = ol.CreateItem(olMailItem)
newMail.Subject = MessageSubject
newMail.Body = MessageBody & vbCrLf
newMail.RecipIents.Add(ToAddress (1))
newMail.RecipIents.Add(ToAddress (2))
newMail.Attachments.Add(MessageAttachment)
newMail.Send
End If
Else
ToAddress (1) = "xxx@email.com"
ToAddress (2) = "xxx@email.com"
MessageSubject = "Shipment/Movement Files"
MessageBody = "We have not received any new files today."
Set ol = WScript.CreateObject("Outlook.Application")
Set ns = ol.getNamespace("MAPI")
Set newMail = ol.CreateItem(olMailItem)
newMail.Subject = MessageSubject
newMail.Body = MessageBody & vbCrLf
newMail.RecipIents.Add(ToAddress (1))
newMail.RecipIents.Add(ToAddress (2))
newMail.Send
End If
Function GetNewestFile(ByVal sPath)
sNewestFile = Null ' init value
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sPath)
Set oFiles = oFolder.Files
' enumerate the files in the folder, finding the newest file
For Each oFile In oFiles
On Error Resume Next
If IsNull(sNewestFile) Then
sNewestFile = oFile.Path
dPrevDate = oFile.DateLastModified
Elseif dPrevDate < oFile.DateLastModified Then
sNewestFile = oFile.Path
End If
On Error Goto 0
Next
If IsNull(sNewestFile) Then sNewestFile = ""
GetNewestFile = sNewestFile
End Function
sPath = "C:\Documents and Settings\Desktop\TestFolder\"
Set oFSO = CreateObject("Scripting.FileSystemObject")
sNewestFile = GetNewestFile(sPath)
If sNewestFile <> "" Then
WScript.Echo "Newest file is " & sNewestFile
dFileModDate = oFSO.GetFile(sNewestFile).DateLastModified
If DateDiff("h", dFileModDate, Now) > 24 Then
Dim ToAddress (2)
Dim FromAddress
Dim MessageSubject
Dim MessageBody
Dim MessageAttachment
Dim ol, ns, newMail
ToAddress (1) = "xxx@email.com"
ToAddress (2) = "xxx@email.com"
MessageSubject = "Shipment/Movement Files"
MessageBody = "The following Shipment and Movement Transactions files have been received."
MessageAttachment = (sNewestFile)
Set ol = WScript.CreateObject("Outlook.Application")
Set ns = ol.getNamespace("MAPI")
Set newMail = ol.CreateItem(olMailItem)
newMail.Subject = MessageSubject
newMail.Body = MessageBody & vbCrLf
newMail.RecipIents.Add(ToAddress (1))
newMail.RecipIents.Add(ToAddress (2))
newMail.Attachments.Add(MessageAttachment)
newMail.Send
End If
Else
ToAddress (1) = "xxx@email.com"
ToAddress (2) = "xxx@email.com"
MessageSubject = "Shipment/Movement Files"
MessageBody = "We have not received any new files today."
Set ol = WScript.CreateObject("Outlook.Application")
Set ns = ol.getNamespace("MAPI")
Set newMail = ol.CreateItem(olMailItem)
newMail.Subject = MessageSubject
newMail.Body = MessageBody & vbCrLf
newMail.RecipIents.Add(ToAddress (1))
newMail.RecipIents.Add(ToAddress (2))
newMail.Send
End If
Function GetNewestFile(ByVal sPath)
sNewestFile = Null ' init value
Set oFSO = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFSO.GetFolder(sPath)
Set oFiles = oFolder.Files
' enumerate the files in the folder, finding the newest file
For Each oFile In oFiles
On Error Resume Next
If IsNull(sNewestFile) Then
sNewestFile = oFile.Path
dPrevDate = oFile.DateLastModified
Elseif dPrevDate < oFile.DateLastModified Then
sNewestFile = oFile.Path
End If
On Error Goto 0
Next
If IsNull(sNewestFile) Then sNewestFile = ""
GetNewestFile = sNewestFile
End Function