Automatically Save New Attachments

I need code to do the following when a new message comes in:

-Check the e-mail for who sent it
-Save the attachments to the desktop

I can write code, but I am unfamiliar with programming in Outlook, so I don't know how to get this code to run.  What event will I have to use, and where do I need to put it?
Who is Participating?
MourdekaiAuthor Commented:
The ItemAdd event never seems to run.  I send myself a test message, and when I receive it, shouldn't the code run?

Also, how do I test the e-mail for the address that sent it?
I have included a macro to do exactly that in my book.

Sub saverep()
Dim oApp As Application ‘set up the variables
Dim oNS As NameSpace
Dim oMessage As Object
      Dim oAttachments As Outlook.Attachments
      Dim NbrMsgs

   ‘ Create new Outlook application
Set oApp = New Outlook.Application  
      Set oNS = oApp.GetNamespace("MAPI")
      Set oFolder = oNS.GetDefaultFolder(olFolderInbox)
      NbrMsgs = 0
      ‘ Cycle through the messages
For Each oMessage In oFolder.Items
With oMessage
            If .SenderName = "TheBoss" Then
               NbrMsgs = NbrMsgs + 1
   "C:\reports\in\" & NbrMsgs & "report.txt"
           End If
             End With
End Sub

It was written by Stefri and the explanation paragraphs are below.

The lines from Sub saverep() through Dim NbrMsgs set up variables needed for use in the rest of the program. Set oApp creates a new Outlook application in memory. The messages that follow it set up functions to open the inbox.
The active part of this macro is the loop through each message in the inbox and the processing of the attachment (if any). This is accomplished with the For Each … Next loop.
The line below For Each allows the code that follows it to examine the contents of the message. The next line checks to see if the sender name is TheBoss. It will look for attachments only in messages sent from TheBoss.
NbrMsgs is incremented to separate one attachment from the next. Notice that it is used in the file name (which is the line below it). The line that actually saves the attachment is next. It is saved in the C:\REPORTS\IN directory and is saved as 1report.txt, 2report.txt, etc.
The next line deletes the message after processing. The final four lines are closing lines for their counterparts (If…Endif, With…End With, For…Next, Sub…End Sub).
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

MourdekaiAuthor Commented:
Is there some way I can set up a rule to run a script that will do this?
This would be attached to a keystroke.  Let's say you get 15 attachments in per day.  Every evening you run the macro against the mailbox and it saves the attachments for you.
Something completely automated causes a SERIOUS security risk.  You are better off with a procedure you have to initiate.
MourdekaiAuthor Commented:
I'm not familiar with all of the security stuff in Outlook, but I fail to see the serious security risk in creating a rule to do this automatically.  I believe that in order to use a macro, I would have to lower the security settings to allow it.  Doesn't that pose a security risk?

I need this to be automatic because the computer skill level of the user makes it hard for them to consistently know how to save an attachment to the same folder on her desktop everytime.  I need to set up Outlook to  receive messages on startup, and then copy any attachments from a specified address to a folder automatically.

Please correct me if I'm wrong on the security issues.  I've programmed plenty, but I've never coded for outlook or created macros for it before.
Okay.  Let's set up a scenario.  Each attachment is automatically saved to the hard drive as it comes in.  The user receives a really destructive virus through email.  Since it is automatically saved and taken out of the email arena there is a comfort and lack of caution when opening these attachments.  The user (due to lack of knowledge) opens a SCR file and BOOOOOOOOOOOOOOOOM.  In a brief, quite unreversible, minute it zaps everything on that machine and emails itself to the entire company.  Because everyone is expecting all attachments to be good, by the end of the day nobody has a computer that works.  You had better have good backups!!!
Wait until that happens to the President of the company.  I hope you have other job prospects!!
Continued ...
If you don't have backups each hard drive has to be sent off to a data recovery service like and costs between $1,000-$2,000 each to partially recover some data.
It is completely up to you, but I always err on the safe side.
MourdekaiAuthor Commented:
So what security does enabling macros ensure me?  If I enable macros, cannot e-mails contain viruses in a macro?

I can circumvent copying virus filled attachments by checking the extension of the file.  There is only one extension I am expecting.
This is from OL2003 help.

Security  Because macros can contain viruses, be careful about running them. Take the following precautions: run up-to-date antivirus software on your computer; set your macro security level to high; clear the Trust all installed add-ins and templates check box; use digital signatures; maintain a list of trusted publishers.

To perform these procedures, you must have Microsoft Internet Explorer version 4.0 or later installed on your computer.

Do one of the following:

Add a macro developer to the list of trusted publishers

If you haven't already done so, set the macro (macro: An action or a set of actions you can use to automate tasks. Macros are recorded in the Visual Basic for Applications programming language.) security level to Medium or High.


On the Tools menu, click Options.
Click the Security tab.
Under Macro Security, click Macro Security.
Click the Security Level tab, and then select the security level you want to use.
Open the file or load the add-in (add-in: A supplemental program that adds custom commands or custom features to Microsoft Office.) that contains macros certified by the macro developer that you want to add to the list.
In the Security Warning box, select the Always trust macros from this publisher check box.
Note  If the Security Warning box does not display the Always trust macros from this publisher check box, the macros are not digitally signed (digital signature: An electronic, encryption-based, secure stamp of authentication on a macro or document. This signature confirms that the macro or document originated from the signer and has not been altered.). You cannot add this macro developer to the list of trusted sources without a digital signature.

I see how it can be done automatically and safely now.  If you sign the macro and trust only that one publisher then no other macros will be able to run.  You could write your own send/receive function to do a send/rcv and then run the macro.
MourdekaiAuthor Commented:
I'm finally getting some code to run.  What is the property that will give me the e-mail address of who sent it?  This is NOT the sendername.
Property SenderEmailAddress As String
    Member of Outlook.MailItem
MourdekaiAuthor Commented:
I don't have that property, I'm using Outlook 2002.
MourdekaiAuthor Commented:
Thanks for the help sunray.  I used the code from your two links to get this working for me.  The main thing I was missing was a reference to the Microsoft CDO library so I could get access to the sender's email address.

For those reviewing this as a PAQ, here is a link with code that will let you get the sender's email address:
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.