Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Andrzejek on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VBSript the gets the latest files then with email files as attachment

Status
Not open for further replies.

TekiePrincess

Programmer
Jun 1, 2009
18
US
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
 
Are you sure that "sNewestFile" has a value? Do you have outlook installed on the machine?
 
Hi Guitarzan,
Yes, I am sure sNewestFile has a value because the following works fine, the popup displays identifying the newestfile name and date last modified:

WScript.Echo "Newest file is " & sNewestFile

Yes, outlook is installed on the machine. If I test the code with just the outlook portion the email sends fine.
 
You should see some kind of popup warning from Outlook saying that another application is trying to use Outlook... which you have to say Yes to... Also, you can check your outbox / sent items to see if the email is there, and just not getting out.
 
I would move the variable declarations out of the If statement, especially if you are using option explicit.

I'm a bit confused. In the first post you say it doesn't send email, but then you say it sends fine with just the outlook code? What code did you add back that causes problems? Is sending an attachment the problem?

Also, the 'newest file' that you are testing with: is it more than 24 hours old?
 
Oh yea i misread the second reply... sounds like it has to be
Code:
 If DateDiff("h", dFileModDate, Now) > 24 Then
that is false... if it is <= 24, you have no Else statement, therefore nothing would be done.
 
Guitarzan,
I do not get that pop-up windows at all after I added the email portion of the code to my get latest file portion. I check the outbox and nothing is there and no new sent items are there either.
 
Jges,
If i just run this portion of the code it works fine.

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

But after adding this to the get newest file, the only thing that happens is the pop-up that tells me the newest filename and modification date.
 
Maybe it will help if I expalin what I am trying to do. I have a folder where daily 2 new files are added to the folder. I am trying to create a script that identifies the latest two files and attach them to an email and send them to multiple people.
 
Hi All,
I have gotten a little further. This code now sends out the email with the newest file attached but I can not get it to send multiple files or to multiple recipients:

Dim fNewest
Dim ToAddress
Dim FromAddress
Dim MessageSubject
Dim MessageBody
Dim MessageAttachment
Dim ol, ns, newMail

set oFolder=createobject("scripting.filesystemobject").getfolder("C:\Documents and Settings\Desktop\TestFolder")
For Each aFile In oFolder.Files
If fNewest = "" Then
Set fNewest = aFile
Else
If fNewest.DateCreated < aFile.DateCreated Then
Set fNewest = aFile
End If
End If
Next
ToAddress = "xxx@email.com;xxxx@email.com"
MessageSubject = "Email Automation Test from desktop"
MessageBody = "This is a test to see if you can receive the message with the two attachments, please send an acknowledgment if you got this message"
MessageAttachment = fNewest
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)
newMail.Attachments.Add(MessageAttachment)
newMail.Send
 
multiple recipients? isnt that just your ToAddress string being delimited with a ; as you have shown?

multiple attachments?, how does the Attachments.Add method work? do you not just call it a few times adding new attachments as you go?
 
Mrmovie,
Multiple recipients - I finally got this to work using the following:

Dim ToAddress(2)

ToAddress(1) = "xxx@email.com"
ToAddress(2) = "xxxx@email.com"

newMail.RecipIents.Add(ToAddress(1))
newMail.RecipIents.Add(ToAddress(2))

multiple attachements - The folder that the attachements comes from will get two files a day at the same time names differently. So I am thinking if I do a newest for each of the different file names then I should be able to attach each using the following type of code:

Dim MessageAttachment(2)

MessageAttachment = fNewestShip
MessageAttachment = fNewestMove

newMail.Attachments.Add(MessageAttachment(1))
newMail.Attachments.Add(MessageAttachment(2))
 
you shouldnt need to pass them by using an array, e.g. this should be ok

newMail.Attachments.Add(fNewestShip)
newMail.Attachments.Add(fNewestMove)

btw, your code should have read (or even (0) and (1))

Dim MessageAttachment(2)

MessageAttachment(1) = fNewestShip
MessageAttachment(2) = fNewestMove

newMail.Attachments.Add(MessageAttachment(1))
newMail.Attachments.Add(MessageAttachment(2))
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top