To make it easier to read:
Sub Attach_your_last_saved_file()
' You need to set a reference to the scripting object
Dim fso As Scripting.FileSystemObject
Dim strFile As String
Dim fsoFile As Scripting.File
Dim fsoFldr As Scripting.Folder
Dim dtNew As Date, sNew As String
Dim oReply As Outlook.MailItem
Dim oItem As Object
Dim signature As String
Dim objNameSpace As NameSpace
Set objNameSpace = Application.GetNamespace("MAPI")
Set fso = New Scripting.FileSystemObject
strFile = "C:\Users\bgyona02\Desktop\outolook_files\"
Set fsoFldr = fso.GetFolder(strFile)
For Each fsoFile In fsoFldr.Files
' check the extension and age
If fsoFile.DateLastModified > dtNew And Right(fsoFile.Name, 5) = ".xlsx" Then
sNew = fsoFile.Path
dtNew = fsoFile.DateLastModified
Debug.Print sNew & amp; dtNew
End If
Next fsoFile
' Create a reference with the excel file
Dim treshold_fileApp As Excel.Application
treshold_file = "C:\Users\bgyona02\Desktop\treshold_file\"
Dim sheet4 As Excel.Worksheet
Set sheet4 = treshold_file.Object.Sheets(4)
Dim obj As New DataObject
Dim txt As String
txt = treshold_file.Worksheets(4).Range("C6")
obj.SetText txt
'Create e-mail item
Dim rngTo As Range
Set objApp = Application
Set oItem = objApp.ActiveExplorer.Selection.Item(1)
If Not oItem Is Nothing Then
Set oReply = oItem.Reply
End If
signature = oReply.HTMLBody
oReply.HTMLBody = signature
With oReply
.BodyFormat = olFormatHTML
.HTMLBody = "<p>First line here<p>Second line here<p/>Third line here.<p>" & "<br />" & signature
.Attachments.Add sNew
'add correct e-mail address based on the excel file
.To = txt
.Display
End With
'assign category
With oItem
.Category = "Test"
.FlagStatus = olFlagComplete
oItem.Save
End With
End Sub