Automated Outlook Attachment Download with Email Date - vba

I am hoping someone in here can help me with this request. I have found code shared by someone at https://www.pixelchef.net/content/rule-autosave-attachment-outlook that would help me setup a rule to automatically download attachments based on a rule.
The code is working fine but I want to tweak it to instead of naming the files like this 2021-08-28 10-00Test to format the name of the file like this Test_2021-08-28 10-00. The code is also at the moment adding the time stamp based on when the rule runs. I want to change it to instead pick up the date and time from the email.
Here is the code:
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat
dateFormat = Format(Now, "yyyy-mm-dd H-mm")
saveFolder = "C:\path"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
Any help is appreciated.

You can use the MailItem.ReceivedTime property which returns a Date indicating the date and time at which the item was received.
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat
dateFormat = Format(itm.ReceivedTime, "yyyy-mm-dd H-mm")
saveFolder = "C:\path"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
There are two additional aspects I've noticed in your code:
Your rule can process all kind of Outlook items - appointments, meeting requests, documents, mail and etc. So, it makes sense to add a type check to prevent any unexpected results when dealing with unsupported item types.
Make sure the file name of the attached file has a unique name to save with. The folder can't contain two files with the same name while emails may contain two attachments with the same display name.

Related

Outlook Rules VBA Run a Script - Multiple Rules

How can I have multiple 'scripts' available to run multiple rules? The only code that appears available to run with Rules/run a script..is the code pasted in ThisOutlookSession. The modules below do not appear.
I have Outlook 2010 on windows 11.
I'm accounts payable for my company.
All my invoices (many!) come in my email.
What I'm trying to do: I have many vendors, I want to create rules that will automatically save the attachments from specific vendors to the correct folder. I want to be able to set a rule to save attachments that come from joeshmo's email to the joeshmo folder in My Documents.
What I CAN do: I have a couple working codes:
I have a code where i can select emails, run the macro and all attachments are saved to the same folder.
I have a code that i can use in a rule.
I did have to re-enable scripts for my version of outlook.
So I go to Rules, I set conditions, choose "run a script" and in the drop down, only 1 code is available to run, and that is whatever is pasted into "ThisOutlookSession". When I open visual basic, there is a list of Modules but none of them appear in Run a script. Nor can I move them up there. I can only paste one code. Below is an example of the code.
I wanted to create a Rule and accompanying script per Vendor to run automatically. So all the incoming emails with invoices are automatically saved to their folder.
Am i asking too much? i can save all attachments to the same folder and then sort.
Or I can create rules to assign categories to each vendors email and then sort alphabetically...meaning, I can select all the "A" vendors, run the macro, Select the "B" vendors, etc.
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat
dateFormat = Format(Now, "yyyy-mm-dd H-mm")
saveFolder = "C:\Users\jenny\Documents\Attachments\outlook testing\test number one"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
Any ideas? thanks
"Run a script" expects the incoming item as input.
Code must start with something like Sub somename(itm As Object).
You could create code for each vendor.
Public Sub saveAttachtoDisk_Sender1(itm As Object)
' Rule conditions: from Sender1 with attachment
Dim objAtt As Attachment
Dim saveFolder As String
Dim dateFormat As String
dateFormat = Format(Now, "yyyy-mm-dd H-mm")
saveFolder = "C:\Users\jenny\Documents\Attachments\outlook testing\test number one"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
Next
End Sub
Public Sub saveAttachtoDisk_Sender2(itm As Object)
' Rule conditions: from Sender2 with attachment
Dim objAtt As Attachment
Dim saveFolder As String
Dim dateFormat As String
dateFormat = Format(Now, "yyyy-mm-dd H-mm")
saveFolder = "C:\Users\jenny\Documents\Attachments\outlook testing\test number two"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
Next
End Sub
A single rule may be preferable:
Public Sub saveAttachtoDisk_VariousSenders(itm As Object)
' Rule condition: with attachment
Dim objAtt As Attachment
Dim saveFolder As String
Dim dateFormat As String
dateFormat = Format(Now, "yyyy-mm-dd H-mm")
Debug.Print itm.senderEmailAddress
Select Case itm.senderEmailAddress
Case "sender1#someplace.com"
saveFolder = "C:\Users\jenny\Documents\Attachments\outlook testing\test number one"
Case "sender2#someplace.com"
saveFolder = "C:\Users\jenny\Documents\Attachments\outlook testing\test number two"
Case Else
Debug.Print itm.senderEmailAddress & " not listed."
End Select
Debug.Print saveFolder
If saveFolder <> "" Then
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
Next
End If
End Sub

Auto-Save Outlook attachments using received date (Minus 1 Calendar Day)

Background: I receive several daily Infection Control Reports in pdf format from our electronic medical records system via Outlook email attachments.
Request: Given the large number of reports, I am trying to find a way to autosave the attachments using an outlook rule.
Currently, the code I use works only to save the attachment with its respective received date. However, these medical reports largely reflect the previous day's data. Therefore, I was wondering, How would I format this code so that it will take the email attachment's received date less (minus) 1 day, and autosave it in a specified location?
Here is what I have so far:
Sub Save_DailyFluReport(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim dateFormat As String
Dim saveFolder As String
dateFormat = Format(itm.ReceivedTime, "dd-mmmm-yyyy")
saveFolder = "Z:\Infection Control\IP Daily Surveillance Reports"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & dateFormat & " - " & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
I figured it out! Hope this solution can help someone else in the future.
Sub Save_DailyFluReport(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim dateFormat As String
Dim saveFolder As String
dateFormat = Format(DateSerial(Year(itm.ReceivedTime), Month(itm.ReceivedTime), Day(itm.ReceivedTime) - 1), "dd-mmmm-yyyy")
saveFolder = "Z:\Infection Control\IP Daily Surveillance Reports"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & dateFormat & " - Daily Flu Report.pdf"
Set objAtt = Nothing
Next
End Sub

Rule to save attachments to file with today's date

I'm trying to put together some VBA that will save an attachment that I get sent daily to a folder on my network, I've got as far as the attachment being saved to the correct location, however, I want to prefix the document with the date in which it was saved.
The attachment is summary.rtf and I'd like it to be 20160805_summary.rtf etc.
My VBA are essentially NOTHING (I'm a SQL girl), so any simple advice would be so greatly appreciated, I've been revisiting this for days and can't find any help anywhere!
My current code looks like this:
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "X:\Tessitura\Shared Full Access\Secure_CXL_Reports"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
I would be so grateful for any extra help!
You need to add the below section into the line, format will change the form of your date to the one required and date will return the current date, change the y/d/m for the format as required.
format(date, "yyyymmdd")
This is the line inserted into your code.
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
saveFolder = "X:\Tessitura\Shared Full Access\Secure_CXL_Reports"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & format(date, "yyyymmdd") & "_" & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub

Auto-save attachments in Outlook 2010

My goal is to be able to save attachments from a specific email address to a folder locally. I have created a VB script but for some reason it doesn't work.
Public Sub saveAttachtoDisk (itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat
dateFormat = Format(Now, "dd-mm-yyyy H-mm")
saveFolder = "c:\temp\"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & dateFormate & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
I've tried to created a new module and a new rule so it will run the script when the message arrives from the specific address. I have also tried to put the script in "ThisOutlookSession" but nothing works.
Did you try to run the script manually under the debugger?
Try to choose another drive. The C: drive requires admin privileges for writing.
You may find the Getting Started with VBA in Outlook 2010 article helpful.

Outlook 2013 script to save attachments then move email to folder

I need a simple script for Outlook 2013 for a mailbox that customers send documents too.
I'm going to set a rule that that applies to all incoming mail. If the email has an attachment, it needs to be save to a folder. Then the mail, that the attachment was saved from, needs to be moved to a completed folder.
I think this is really simple, but I don't know much about VBA or Outlook scripting. I found this script, which removes the attachment and adds a date to the saved file, which is perfect.
I just need it to then move the completed email to a folder.
hopefully someone can help out.
Public Sub saveAttachtoDisk(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
Dim saveFolder As String
Dim dateFormat
dateFormat = Format(Now, "yyyy-mm-dd H-mm")
saveFolder = "\\myfilepath\"
For Each objAtt In itm.Attachments
objAtt.SaveAsFile saveFolder & "\" & dateFormat & objAtt.DisplayName
Set objAtt = Nothing
Next
End Sub
The Movemethod of the MailItem class can be used to get the job done.