I am trying to send the most recent PDF files in a folder from Excel using VBA.
I have managed to do it in Outlook VBA - I am not sure what needs to change to do it in Excel. The reason is because the Outlook macro conflicts with Excel macros that are running periodically.
My code at the moment just attaches all the files in a folder that have been created in the last 30 secs - only ever one PDF.
Please note that the code works perfectly in Outlook.
Sub SendFiles()
Dim objMail As Outlook.MailItem
Dim fso As Object
Dim strFile As String
Dim fsoFile
Dim fsoFldr
Dim dtNew As Date, sNew As String
Set fso = CreateObject("Scripting.FileSystemObject")
strFile = "C:\temp\" 'path to folder
Set fsoFldr = fso.GetFolder(strFile)
dtNew = Now - TimeValue(00:00:30) '30 seconds ago
For Each fsoFile In fsoFldr.Files
If fsoFile.DateCreated > dtNew Then
sNew = fsoFile.Path
Set objMail = Application.CreateItem(olMailItem)
With objMail
.To = "[email protected]"
.Subject = "Example"
.BodyFormat = olFormatPlain
.Attachments.Add sNew
.Importance = olImportanceHigh
.Send
End With
End If
Next fsoFile
End Sub