none
Sending files by email with subject RRS feed

  • Question

  • I have used the following code to send files by email with subject but I get the following error Run-time error "424": Object required

    Could someone help me to fix this error. Thank you very much.

    Set fso = CreateObject("Scripting.FileSystemObject")
       
      ' path to folder
     strFile = "C:\Users\Diane\"
         
     Set fsoFldr = fso.GetFolder(strFile)
     dtNew = Now - 0.25 ' 6 hours ago
          
    For Each fsoFile In fsoFldr.Files
     
    ' if date created is less than 6 hours ago
    ' can use .DateLastModified
    If fsoFile.DateCreated > dtNew Then
     
    sNew = fsoFile.Path
               
    Set objMail = Application.CreateItem(olMailItem)

     With objMail
     .To = "alias@domain.com"
     .BodyFormat = olFormatPlain
     .Attachments.Add sNew
     .Display ' .send
     End With
     
    Set olMsg = olApp.CreateItem(0) ' email

    ' send message
     
    With olMsg
     .Subject = "Here's that file you wanted"
     .To = "alias@domain.com"
     .HTMLBody = "Hi " & olMsg.To & "," & vbCrLf & "Attached is " & fName & " you requested."
     .Send
    End With

    End If
    Next fsoFile
       
    End Sub

    Monday, July 4, 2016 1:53 PM

Answers

All replies