Outlook – Automatically save attachments from mail on a network drive

microsoft-outlookvba

Every morning I receive an automatically generated e-mail from a server, it includes a spreadsheet attachment.
I made a rule in outlook that captures these emails. Now, I would like to make a macro to rename the attached file (. Xls) to =today() and saves it in a folder on a network drive. This macro must run every weekday at 8:00.
I've made a lot of VBA in Excel, but have no experience in outlook.
Might this be possible? And does anyone know how to do it?

Kind regards
Staal

Best Answer

Why delay the save until 8:00?

In the rule that "captures these emails" save with "run a script".

Sub saveAttachtoDisk(itm As Outlook.MailItem)

Dim objAtt As Outlook.Attachment
Dim saveFolder As String

saveFolder = "D:\temp"  ' change to your path

For Each objAtt In itm.Attachments
    objAtt.SaveAsFile saveFolder & "\" & Format(Now, "yyyy-mm-dd") & ".xls"
Next

End Sub

Edit based on comment:

Not sure you would want to do this inside saveAttachtoDisk but to manipulate the Workbook once it is saved, there is Excel.

Try GetObject, if there is an error then use CreateObject("Excel.Application") http://support.microsoft.com/kb/288902

Then something like this, or whatever way works for you since you know Excel VBA.

Workbooks.Open(name of file)

Worksheets(1).Name = "Sheet 1"


Edit 2 based on comment:

You have to pass a parameter.

Open a mailitem then F8 here.

Sub saveAttachtoDisk_test
Dim currItem as mailitem
set curritem = activeinspector.currentitem
saveAttachtoDisk curritem
set currItem = nothing
end sub
Related Question