Outlook macro to download attachment and append timestamp after filename

Yogibear88

New Member
Joined
Sep 28, 2017
Messages
6
Hi all,

I have a code that appends the timestamp to the file attachment. Currently the timestamp is placed before the filename but when I tried to modify the code to insert timestamp after the filename, it changes the file extension and the file attachment becomes unreadable. I know that there is this code using InStrRev (Fname, ".") but I am not too familiar with the coding syntax and could not get it to work.

Appreciate if someone can help.

VBA Code:
Public Sub saveAttachToDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat As String

saveFolder = "D:\Users\bob\TEMP\Daily Report"
dateFormat = Format(Now, "yyyy-mm-dd")

 For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & dateFormat & "_" & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
 

Excel Facts

Whats the difference between CONCAT and CONCATENATE?
The newer CONCAT function can reference a range of cells. =CONCATENATE(A1,A2,A3,A4,A5) becomes =CONCAT(A1:A5)
Try this:
VBA Code:
Dim p As Long
p = InStrRev(objAtt.DisplayName, ".")
objAtt.SaveAsFile saveFolder & "\" & Left(objAtt.DisplayName, p - 1) & Format(Now, "_yyyy-mm-dd") & Mid(objAtt.DisplayName, p)
 
Upvote 0

Forum statistics

Threads
1,224,814
Messages
6,181,128
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top