user2787768
user2787768

Reputation: 1

Saving Outlook attachment with date in the filename

All I want to do is very simply set a date variable in VBA in the following code. However if I try to use mydate = date, it doesn't work:

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment

Dim saveFolder As String
Dim mydate As String

mydate = date

saveFolder = "T:\EC Portfolio Reports\CCA Credit Europe\ctpty"
    For Each objAtt In itm.Attachments
    objAtt.SaveAsFile saveFolder & "\" & mydate & objAtt.FileName
    Set objAtt = Nothing
Next
End Sub

Upvotes: 0

Views: 1426

Answers (1)

SeanC
SeanC

Reputation: 15923

to have the date as a string, use format. This will also allow you to remove any illegal characters in the date, as date will return slashes - Filenames cannot contain /:*?"<>|

use mydate = format(date,"yyyymmdd")

Upvotes: 1

Related Questions