PDA

View Full Version : Outlook VBA macro for multiple file attachment according to filename string



d1srupt3r
10-30-2018, 03:48 AM
I have a working macro for Outlook wherein it will create new email. However, I need some several files that are needed to be attached from a specific local folder and the files attached always has current date on the file naming (FILE1_ddmmyyyy). Example: FILE1_30102018.xlsx, FILE2_30102018.xlsx
Below is the code I have right now and I can't figure out how to auto-attach the files with the filename "*30102018.xlsx":


Sub FileDraft()
Dim obApp AsObject
Dim NewMail As MailItem

'Format(Date, "ddmmyyyy")
Dim szTodayDate AsString
szTodayDate =Date
Dim szNextDate AsString
Dim LWeekday AsInteger
LWeekday = Weekday(szTodayDate, vbSunday)

If LWeekday ="5"Then
szNextDate = DateAdd("d",3, szTodayDate)
Else
szNextDate = DateAdd("d",1, szTodayDate)
EndIf
Dim szNextDatereformat AsString
szNextDatereformat = Format(szNextDate,"ddmmyyyy")

Set obApp = Outlook.Application
Set NewMail = obApp.CreateItem(olMailItem)

'You can change the concrete info as per your needs
With NewMail
.Subject ="FILES_"& szNextDatereformat
.To="Recipient_Address"
.CC ="contacts_on_the_CC"
.Body ="messageBodyhere"
.Attachments.Add ("C:\Attachments\FILE1_30102018.xlsx")
.Importance = olImportanceHigh
.Display
EndWith

Set obApp =Nothing
Set NewMail =Nothing
EndSub