Option
Explicit
Private
Const
FOLDER_PATH_1
As
String
=
"C:\Users\hhwjanse\Desktop\tmp"
Private
Const
FOLDER_PATH_2
As
String
=
"C:\Users\hhwjanse\Desktop\tmp\Neuer Ordner"
Private
Const
FILE_EXTENSION
As
String
=
"txt"
Private
Const
MAIL_TO
As
String
=
"test@domain.de"
Private
Const
MAIL_SUBJECT
As
String
=
"Test"
Private
Const
MAIL_BODYTEXT
As
String
=
"Testtext"
Public
Sub
create_mail()
Dim
strAttachment_1
As
String
, strAttachment_2
As
String
Dim
appOut
As
New
Outlook.Application
Dim
outMail
As
Outlook.MailItem
strAttachment_1 = get_path_of_newest_file(FOLDER_PATH_1, FILE_EXTENSION)
strAttachment_2 = get_path_of_newest_file(FOLDER_PATH_2, FILE_EXTENSION)
Set
outMail = appOut.CreateItem(olMailItem)
With
outMail
.
To
= MAIL_TO
.SUBJECT = MAIL_SUBJECT
.Body = MAIL_BODYTEXT
If
Not
strAttachment_1 = vbNullString
Then
.Attachments.Add strAttachment_1
If
Not
strAttachment_2 = vbNullString
Then
.Attachments.Add strAttachment_2
.Display
End
With
Set
outMail =
Nothing
Set
appOut =
Nothing
End
Sub
Private
Function
get_path_of_newest_file(
ByVal
FolderPath
As
String
,
Optional
ByVal
FileExtension
As
String
= vbNullString)
As
String
Dim
fso
As
New
FileSystemObject
Dim
f
As
File, fTmp
As
File
Dim
strFilePath
As
String
If
fso.FolderExists(FolderPath)
Then
For
Each
f
In
fso.GetFolder(FolderPath).Files
If
Not
FileExtension = vbNullString
Then
If
fso.GetExtensionName(f.Path) = FileExtension
Then
If
fTmp
Is
Nothing
Then
Set
fTmp = f
Else
If
f.DateLastModified > fTmp.DateLastModified
Then
Set
fTmp = f
End
If
End
If
End
If
Else
If
fTmp
Is
Nothing
Then
Set
fTmp = f
Else
If
f.DateLastModified > fTmp.DateLastModified
Then
Set
fTmp = f
End
If
End
If
End
If
Next
f
strFilePath = fTmp.Path
Else
strFilePath = vbNullString
End
If
get_path_of_newest_file = strFilePath
Set
f =
Nothing
Set
fTmp =
Nothing
Set
fso =
Nothing
End
Function