1

I'm trying to put together some VBA that will save an attachment that I get sent daily to a folder on my network, I've got as far as the attachment being saved to the correct location, however, I want to prefix the document with the date in which it was saved.

The attachment is summary.rtf and I'd like it to be 20160805_summary.rtf etc.

My VBA are essentially NOTHING (I'm a SQL girl), so any simple advice would be so greatly appreciated, I've been revisiting this for days and can't find any help anywhere!

My current code looks like this:

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "X:\Tessitura\Shared Full Access\Secure_CXL_Reports"
     For Each objAtt In itm.Attachments
          objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
          Set objAtt = Nothing
     Next
End Sub

I would be so grateful for any extra help!

0m3r
  • 12,286
  • 15
  • 35
  • 71
Anna Coles
  • 11
  • 2

1 Answers1

1

You need to add the below section into the line, format will change the form of your date to the one required and date will return the current date, change the y/d/m for the format as required.

format(date, "yyyymmdd")

This is the line inserted into your code.

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "X:\Tessitura\Shared Full Access\Secure_CXL_Reports"
     For Each objAtt In itm.Attachments
          objAtt.SaveAsFile saveFolder & "\" & format(date, "yyyymmdd") & "_" & objAtt.DisplayName
          Set objAtt = Nothing
     Next
End Sub
Preston
  • 7,399
  • 8
  • 54
  • 84