Avatar of Allen Pitts
Allen Pitts
Flag 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
Visual Basic Classic

Avatar of undefined
Last Comment
GeoffHarper

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
GeoffHarper

THIS SOLUTION ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
GET A PERSONALIZED SOLUTION
Ask your own question & get feedback from real experts
Find out why thousands trust the EE community with their toughest problems.
Allen Pitts

ASKER
Doesn't work
GeoffHarper

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)




Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy