Allen Pitts
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.x xx?
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_Wor kforce_Con solidation \email_att ach\"
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.ReceivedTim e, "_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
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.x
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_Wor
strID = MyMail.EntryID
Set objNS = Application.GetNamespace("
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.ReceivedTim
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
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Actually, I missed the part about flipping the date to the front.
So, it would be:
save_name = Format(objMail.ReceivedTim e, "yyyy-mm-dd-hhnn") & "_" & save_name
(this one line replaces the three "save_name = ..." lines)
So, it would be:
save_name = Format(objMail.ReceivedTim
(this one line replaces the three "save_name = ..." lines)
ASKER