troubleshooting Question

Outlook VBA script rights email attachment to folder, filename format

Avatar of Allen Pitts
Allen PittsFlag for United States of America asked on
Visual Basic Classic
3 Comments1 Solution536 ViewsLast Modified:
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
GeoffHarper

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 3 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 3 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros