Link to home
Start Free TrialLog in
Avatar of Allen Pitts
Allen PittsFlag for United States of America

asked on

Outlook VBA script rights email attachment to folder, filename format

Good afternoon expert,
Code copied below works. Was able to hack the date format  
so instead of mm-dd-yyyy the dat format is yyyy-mm-dd
which does a better job of listing the attchmetns in chronological order.

But I would like to have the date and time at the beginning of the
file name instaed of at the end. I have played with the code for about
an hour and I am starting to get a little crazy.

Can you tell me how to make the file name of the attachment be
yyyy-mm-dd-hhmm_filename.xxx?

Thanks.

Allen in Dallas


++++++++++++beginning of code+++++++++++++++
Sub SaveToFolder(MyMail As MailItem)
Dim strID As String
Dim objNS As Outlook.NameSpace
Dim objMail As Outlook.MailItem
Dim objAtt As Outlook.Attachment
Dim c As Integer
Dim save_name As String
'Place path to sav to on next line. Note that you must include the
'final backslash
Const save_path As String = "F:\Library\Contingent_Workforce_Consolidation\email_attach\"

strID = MyMail.EntryID
Set objNS = Application.GetNamespace("MAPI")
Set objMail = objNS.GetItemFromID(strID)

If objMail.Attachments.Count > 0 Then
For c = 1 To objMail.Attachments.Count
Set objAtt = objMail.Attachments(c)
save_name = Left(objAtt.FileName, Len(objAtt.FileName) - 5)
save_name = save_name & Format(objMail.ReceivedTime, "_yyyy-mm-dd-hhmm")
save_name = save_name & Right(objAtt.FileName, 5)
objAtt.SaveAsFile save_path & save_name

Next
End If

Set objAtt = Nothing
Set objMail = Nothing
Set objNS = Nothing
End Sub
ASKER CERTIFIED SOLUTION
Avatar of GeoffHarper
GeoffHarper
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Allen Pitts

ASKER

Doesn't work
Actually, I missed the part about flipping the date to the front.

So, it would be:

save_name = Format(objMail.ReceivedTime, "yyyy-mm-dd-hhnn") & "_" & save_name

(this one line replaces the three "save_name = ..." lines)