appending 'Date Modified' to autosave script

Status
Not open for further replies.

ciru

Member
Outlook version
Outlook 2010 32 bit
Email Account
Exchange Server 2010
Hoping someone can help. I currently have a outlook script that basically extracts my attachments and saves it to a specific folder. The script works fine, but instead of having the Now() date being appended to the file name when it is saved, I want it to append the Date Modified. I have surfed the net and cant seem to find anything. Hoping someone can help modify the logic below to do this. Thanks for your time in advance. - Ciru

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
dateFormat = Format(Now, "yyyy-mm-dd")

saveFolder = "C:\Desktop\Test999"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & dateFormat & " " & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
 
The attachment's modified date? you need to use FSO to grab that, i'll look for some code. If you want the message received date, use itm.recievedtime.
 
Hi - I am not sure what FSO is, fairly new to this. I actually want the modified date that can seen when viewing a file via explorer. Just need the date and not the time stamp. Thank you for your time in advance.
 
FSO = file system object. Won't the modified date be the same as the sent date? If so, you just need the sent or received date off the message. Sent would be more accurate, but the differences should only be a few minutes apart.
 
Hi Larry,
Actually the modified date can be a different date then the sent/received date. The file could have been created on a different date then when the email with the attached file was sent. I just want the modified date. Modified date will tell me exactly was created. Thanks.
 
I tested that theory and saved an excel attachment file that was in one of my outlook emails to my desktop. After the save, i checked the properties of that file via my explorer and the excel file had the Modified Date. It did not appear to have lost the properties of the file. Hoping someone can still help with how I can modify the Outlook script below to pull in this modified date. Thanks!

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
dateFormat = Format(Now, "yyyy-mm-dd")

saveFolder = "C:\Desktop\Test999"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & dateFormat & " " & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
 
Here is more on FSO: http://msdn.microsoft.com/en-us/library/aa711216(v=vs.71).aspx

You'll need to Dim and Set the filesystem object, then get the modified date from the attachment and change the name. I'm not sure if you can get it from the attachment before its saved; if you need to get it later, you'll change the name as there is not a rename method - it would be like objAtt.name = newfilename
set fso = CreateObject("Scripting.FileSystemObject")
set file = fso.GetFile(objAtt.name)
' do whatever
set file = nothing
set fso = nothing
 
Hi Larry,
I am not sure how to incorporate your suggestions to extract the Modified Date from an attachment in place of the current DateFormat variable I have within the script in my earlier post. I am fairly new to this and found that script while searching online. It makes sense, but not sure how VB actually works in order to incorporate your comments into my scripts logic. Not sure if you can help with the coding, but it would be very much appreciated if you can. If not, thank you none the less.
 
This is probably not complete, but this should give you a general idea of what you need to do and where to put it, you just need the correct code.

Code:
Public Sub saveAttachtoDisk(itm As Outlook.MailItem) 
Dim objAtt As Outlook.Attachment 
Dim saveFolder As String 
dim fso 
dim file 
 
set fso = CreateObject("Scripting.FileSystemObject") 
 
saveFolder = "C:\Desktop\Test999" 
 
For Each objAtt In itm.Attachments 
 
set file = fso.GetFile(objAtt.name)
   dateFormat = Format(file.DateLastModified, "yyyy-mm-dd") 
 
objAtt.SaveAsFile saveFolder & "\" & dateFormat & " " & objAtt.DisplayName 
Set objAtt = Nothing 
Next 
End Sub
 
Try this. This gets the last modified date from the attachment. For every attachment in my inbox, it is the sent date. You can test this with one or more selected messages - to use in a rule, replace the sub name with the name from your macro and remove the starred lines

Code:
Public Sub saveAttachtoDisk() 
Dim itm As Outlook.MailItem ' * 
Dim currentExplorer As Explorer '* 
Dim Selection As Selection '* 
Dim objAtt As Outlook.Attachment 
Dim saveFolder As String 
Dim fso As Object 
Dim newName As Object 
 
Dim file As String 
 
Set currentExplorer = Application.ActiveExplorer '* 
Set Selection = currentExplorer.Selection '* 
 
Set fso = CreateObject("Scripting.FileSystemObject") 
 
For Each itm In Selection'* 
 
saveFolder = "C:\Users\Diane\.tresorit\Tresors\Attachments\" 
For Each objAtt In itm.Attachments 
  
 
file = saveFolder & objAtt.DisplayName 
Set newName = fso.GetFile(file) 
file = Format(newName.DateLastModified, "yyyy-mm-dd ") 
objAtt.SaveAsFile saveFolder & file & objAtt.DisplayName 
Set objAtt = Nothing 
Next 
Next'* 
 
Set fso= Nothing 
End Sub
 
the macro above works with excel (should work with word), but not images or pdf. This one will rewrite the file name after saving it and getting the modified date from the file. It will work with all file types (AFAIK). Same deal as with the other one - test it on selected messages, remove the starred lines and change the name to use in a rule.

Code:
Public Sub saveAttachtoDisk() 
Dim itm As Outlook.MailItem '* 
Dim currentExplorer As Explorer '* 
Dim Selection As Selection'* 
Dim objAtt As Outlook.Attachment 
Dim saveFolder As String 
Dim fso As Object 
Dim oldName 'As Object 
 
Dim file As String 
Dim DateFormat As String 
Dim newName As String 
 
Set currentExplorer = Application.ActiveExplorer'* 
Set Selection = currentExplorer.Selection'* 
 
Set fso = CreateObject("Scripting.FileSystemObject") 
On Error Resume Next 
For Each itm In Selection'* 
 
saveFolder = "C:\Users\Diane\.tresorit\Tresors\Attachments\test\" 
For Each objAtt In itm.Attachments 
  
 
file = saveFolder & objAtt.DisplayName 
objAtt.SaveAsFile file 
Set oldName = fso.GetFile(file) 
DateFormat = Format(oldName.DateLastModified, "yyyy-mm-dd ") 
newName = DateFormat & objAtt.DisplayName 
oldName.Name = newName 
 
Set objAtt = Nothing 
Next 
Next'* 
Set fso= Nothing 
End Sub
 
Hi Diane,

I took the script you provided and removed all the lines with * behind it (see below). I created an outlook rule so that it runs this script each time it receives an email with certain words in the subject and to file then the email to a specific folder in my outlook email account. When I test it out, the script doesn't appear to run but I know the rule does execute it because the email was moved to the folder. I am not sure if the script is failing cause I do not get any error messages. Thoughts?

p.s. I had to add the (itm As Outlook.MailItem) otherwise outlook didn't recognize the script when I created the rule to run this script. Not sure if this is the problem??? Thanks again for you time.

Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim fso As Object
Dim oldName As Object
Dim file As String
Dim DateFormat As String
Dim newName As String


Set fso = CreateObject("Scripting.FileSystemObject")
On Error Resume Next


saveFolder = "C:\desktop\Test999\"
For Each objAtt In itm.Attachments
Set file = saveFolder & objAtt.DisplayName
objAtt.SaveAsFile file
Set oldName = fso.GetFile(file)
DateFormat = Format(oldName.DateLastModified, "yyyy-mm-dd ")
Set newName = DateFormat & objAtt.DisplayName
Set oldName.Name = newName
Set objAtt = Nothing
Next
Set fso = Nothing
End Sub
 
Yeah, it was ok to add itm as mailitem back in the macro name - you need it there for rules but outside if you are running it manually to test.

The Rule should only have conditions in it, no actions. If you want to move the message (or do any other action), do it in the script.

Code:
Dim movetofolder As Outlook.Folder 
 
movetofolder =  Session.GetDefaultFolder(olFolderInbox).Folders("Folder") 
itm.move movetofolder
 
So I modified the outlook rule and removed the action so it only has conditions. Ran the script as noted above, but still doesn't appear to execute it for when I check the path (c:\desktop\test999) where it was suppose to save the attachement(s) they are not there. Not sure what is happening??
 
I'm assuming the path exists? Is it adding them to desktop, named test999date-name ? It shouldn't , since you have the slash on the file name: saveFolder = "C:\desktop\Test999\" (I won't mention how many times I've done that. :))

Two things to try:
Remove the on error resume next so it stops if it fails.
Does the macro I posted work if you select a message that has an attachment and run it?
 
The script doesn't seem to be running at all. I commented out the on error resume next and it appears the file is not being saved either to the C:\desktop\test999\ path. I changed the path to something different just to double check but nothing is happening. So confused as to what is going on.
 
Did you try using my documents path? The desktop should be writable, but maybe it's not.

Did you try the macro in #12 ? That works here - if it doesn't work there, then we know it's not the basic code.
 
Yes. I actually have tried My Documents path and various other paths I have access to. The script I used was the one in #13 . Diane had noted that I needed to remove all the lines with an * from the script in #12 thus the result was the script in #13. Still not working on this end for some reason - I don't even get any errors.
 
First, thank you for the script work. After trying several variations around the web, this script actually worked. Too well. I am drowning in 500+ emails a day. I have setup some automation, but my biggest issue has always been the attachments. For example, through today, I have 489 documents generated by this script, and yet another 811 non-wanted files (.jpg, .png, etc.). My question to you excellent people is how to use the above script to cull out non-wanted files/attachments. Thoughts?
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
M Appending text to the subject line of a Reply, Foward or new Email Outlook VBA and Custom Forms 2
H Macro to Delete Duplicate items in Outlook calendar where title is the same and date is the same Outlook VBA and Custom Forms 0
F Outlook 365 Group INBOX by date but not by date and time Using Outlook 1
S Displaying Date Value In Formula Outlook VBA and Custom Forms 6
CWM330 Wrong Date Using Outlook 4
C Outlook 365 Copy/Save Emails in Folder Outside Outlook to Show Date Sender Recipient Subject in Header Using Outlook 0
R unable to filter tasks when start date is on or before today Using Outlook 4
S Combination Field (Date Format) Outlook VBA and Custom Forms 0
U How can the Received date be edited? Using Outlook 0
D This folder up to date vs all folders up to date Using Outlook 1
S Outlook Macro for [Date][Subject] Using Outlook 1
N Save emails within a certain date range to network drive Outlook VBA and Custom Forms 0
kburrows Reset Date to Keep Tasks from Archiving Using Outlook 9
M Extract "Date sent" from emails (saved to folder using drag and drop) Outlook VBA and Custom Forms 1
DDB VBA to Auto Insert Date and Time in the signature Outlook VBA and Custom Forms 2
P Short Date Format when typing in a task Using Outlook 2
S Change "This Week" flag start date behavior Using Outlook 1
S Archiving and Likely Modified Date Problem Using Outlook 3
A VBA Script - Print Date between first email in Category X and last email in Category Y Outlook VBA and Custom Forms 3
S Outlook 2016 Arrange tasks by date, additional custom sorting, but still use friendly terms like Today, Tomorrow, This week? Using Outlook 1
L Macro to add Date & Time etc to "drag to save" e-mails Outlook VBA and Custom Forms 17
S save attachment with date & time mentioned inside the file Outlook VBA and Custom Forms 0
Witzker Pls help to change the code for inserting date in Ol contact body Outlook VBA and Custom Forms 5
C Outlook with Office365 - search across account, by date rate, in multiple folders - how? Using Outlook 2
M Sorting by Day in Date Column Advanced Filter BCM (Business Contact Manager) 1
V Date and/or time error in Outlook Form Outlook VBA and Custom Forms 0
S Body text of Email from invite date/time Outlook VBA and Custom Forms 7
V 10 Years calenders -single date together Exchange Server Administration 8
O Export Outlook calendar appointments by filters and date range Outlook VBA and Custom Forms 1
D Add date next to day name in Outlook Today calendar view Using Outlook 1
X If you change expiration date of repeated task it dupplicates Using Outlook 1
D Archive by receive date not working Using Outlook 2
V Making a Date field mandatory in outlook form Outlook VBA and Custom Forms 2
P Auto Insert Current Date or Time into Email Subject Outlook VBA and Custom Forms 2
L Ctrl Alt d date stamp use Using Outlook 1
A Create date folder and move messages daily Outlook VBA and Custom Forms 1
Witzker Outlook 2010 Insert Date & Time at the button of an OL contactform in red Using Outlook 2
O Tasks - Is there a postponed date column? Using Outlook 7
J Command Button to stamp a date and time in a textbox in Outlook 2016 Outlook VBA and Custom Forms 3
K Outlook Archive to PST Files by Date Range VBA Script? Outlook VBA and Custom Forms 1
W Save and rename outlook email attachments to include domain name & date received Outlook VBA and Custom Forms 4
F Finding Meetings/Tasks in a date range Using Outlook 1
W Save Outlook attachment in network folder and rename to current date and time Outlook VBA and Custom Forms 18
M Macro to add date/time stamp to subject Outlook VBA and Custom Forms 4
V Changing default date for task follow-up buttons Using Outlook 2
J Old unread emails on current date (MDaemon Server) Using Outlook 1
D Outlook macro with today's date in subject and paste clipboard in body Outlook VBA and Custom Forms 1
C Need VBA code to automatically save message outside outlook and add date Outlook VBA and Custom Forms 1
B Search by date macro Outlook VBA and Custom Forms 0
S set a flag task date as the next weekday Outlook VBA and Custom Forms 4

Similar threads

Back
Top