-
Solved: Send out Attachments that are last modified files in folders
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 ?
[VBA]
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
[/VBA]
-
Here's a function that does what you need...[VBA]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[/VBA]
Call it like this[VBA].Attachments.Add GetLasestFile("S:\Sand\")[/VBA]
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
K :-)
-
The code works perfectly!. Hopefuly one day I could be a good coder as you are.
Thank You!
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules