Consulting

Results 1 to 3 of 3

Thread: Solved: Send out Attachments that are last modified files in folders

  1. #1
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location

    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]

  2. #2
    VBAX Master Killian's Avatar
    Joined
    Nov 2004
    Location
    London
    Posts
    1,132
    Location
    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 :-)

  3. #3
    VBAX Expert Shazam's Avatar
    Joined
    Sep 2005
    Posts
    530
    Location
    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
  •