PDA

View Full Version : Solved: Send out Attachments that are last modified files in folders



Shazam
11-04-2005, 07:18 AM
Hi everyone,

Well I have this code here to send out a attachments daily, My problem is I would like to send out multiple attachments that is last modified files in the folders. Does anyone has a code for that ?




Sub Send()
Dim objol As New Outlook.Application
Dim objmail As MailItem
Set objol = New Outlook.Application
Set objmail = objol.CreateItem(olMailItem)
With objmail
.To = ("Sand") ' Name of distribution list in Contacts 'Add Somebody on Fridays
If Weekday(Date) = 6 Then objmail.Recipients.Add ("anyone@home.com") 'Name of somebody@hotmail.com Contact

.Subject = "Sand Lab Data"
.Body = ""
.NoAging = True
.Attachments.Add "S:\Sand\"
.Attachments.Add "C:\Test\"
.Attachments.Add "Z:\Metal\"
.Display
End With
Set objmail = Nothing
Set objol = Nothing
SendKeys "%{s}", True
End Sub

Killian
11-04-2005, 08:04 AM
Here's a function that does what you need...Function GetLasestFile(strFolderPath As String) As String

Dim strFileName As String
Dim strLatestFileName As String

'find the first file and init variables
strFileName = Dir(strFolderPath, vbNormal)
If strFileName <> "" Then
strLatestFileName = strFileName
End If
Do While strFileName <> "" 'loop though directory
If strFileName <> "." And strFileName <> ".." Then
If (GetAttr(strFolderPath & strFileName) And vbNormal) = vbNormal Then
If FileDateTime(strFolderPath & strFileName) > _
FileDateTime(strFolderPath & strLatestFileName) Then
strLatestFileName = strFileName
End If
End If ' it represents a directory.
End If
strFileName = Dir ' Get next entry.
Loop
GetLasestFile = strFolderPath & strLatestFileName

End Function
Call it like this.Attachments.Add GetLasestFile("S:\Sand\")
Note that if no files are found it will return an empty string, so if that may happen you should include a test for that

Shazam
11-04-2005, 01:21 PM
The code works perfectly!. Hopefuly one day I could be a good coder as you are.

Thank You!