Results 1 to 3 of 3

Thread: Attach the most recent .XML file located at C:\Users\ then email

Threaded View

Previous Post Previous Post   Next Post Next Post
  1. #3
    VBAX Expert Logit's Avatar
    Joined
    Sep 2016
    Posts
    623
    Location
    .
    Here is another method using your code :

    Option Explicit
    
    
    Sub EmailLatestFileInFolder()
    
    
        'Declare the variables
        Dim MyPath As String
        Dim MyFile As String
        Dim LatestFile As String
        Dim LatestDate As Date
        Dim LMD As Date
        
        'Specify the path to the folder
        MyPath = "C:\Users\My\Downloads\"
        
        'Make sure that the path ends in a backslash
        If Right(MyPath, 1) <> "\" Then MyPath = MyPath & "\"
        
        'Get the first Excel file from the folder
        MyFile = Dir(MyPath & "*.xl*", vbNormal)
        
        'If no files were found, exit the sub
        If Len(MyFile) = 0 Then
            MsgBox "No files were found...", vbExclamation
            Exit Sub
        End If
        
        'Loop through each Excel file in the folder
        Do While Len(MyFile) > 0
        
            'Assign the date/time of the current file to a variable
            LMD = FileDateTime(MyPath & MyFile)
            
            'If the date/time of the current file is greater than the latest
            'recorded date, assign its filename and date/time to variables
            If LMD > LatestDate Then
                LatestFile = MyFile
                LatestDate = LMD
            End If
            
            'Get the next Excel file from the folder
            MyFile = Dir
            
        Loop
      
    Dim OApp As Object
    Dim OMail As Object
    Dim signature As String
    
    
    Set OApp = CreateObject("Outlook.Application")
    Set OMail = OApp.CreateItem(0)
    With OMail
    .Display
    End With
    signature = OMail.body
    With OMail
    .To = "Who It May Concern@email.com"
    .Subject = "Please See Attached Document"
    .Attachments.Add MyPath & LatestFile
    .body = "FYI" & vbCrLf & vbCrLf & "signature"
    .Display
    End With
    
    
    On Error GoTo 0
    
    
    Set OMail = Nothing
    Set OApp = Nothing
            
    End Sub
    Attached Files Attached Files

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •