Automatically Save New Attachments

Posted on 2004-04-19
Last Modified: 2010-04-08
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?
Question by:Mourdekai
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
LVL 49

Accepted Solution

sunray_2003 earned 288 total points
ID: 10863362

Author Comment

ID: 10863492
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?
LVL 23

Expert Comment

ID: 10872969
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).
SendBlaster Pro 4 - Bulk Email Sending Software

SendBlaster 4 Pro - Best Bulk Emailing Sending Software
Automatic Subscribe / Unsubscribe Processing
Great for Newsletters & Mass Mailings
Optional HTML & Text Composition
Integration with Google Features
Built in Spam Score Checking
Free Professional Templates - Feature Packed!


Author Comment

ID: 10879775
Is there some way I can set up a rule to run a script that will do this?
LVL 23

Expert Comment

ID: 10883346
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.

Author Comment

ID: 10889667
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.
LVL 23

Expert Comment

ID: 10890500
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!!
LVL 23

Expert Comment

ID: 10890533
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.

Author Comment

ID: 10890871
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.
LVL 23

Expert Comment

ID: 10895072
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.

Author Comment

ID: 10933193
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.
LVL 23

Expert Comment

ID: 10937292
Property SenderEmailAddress As String
    Member of Outlook.MailItem

Author Comment

ID: 10943173
I don't have that property, I'm using Outlook 2002.

Author Comment

ID: 10943734
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:

Featured Post

Free learning courses: Active Directory Deep Dive

Get a firm grasp on your IT environment when you learn Active Directory best practices with Veeam! Watch all, or choose any amount, of this three-part webinar series to improve your skills. From the basics to virtualization and backup, we got you covered.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What does UTC stand for?  “Coordinated Universal Time” – Think of this as the true time on Planet Earth that never changes with the exception of minor leap seconds here and there to account for the changes in the planet's rotation.   What does th…
Is your Office 365 signature not working the way you want it to? Are signature updates taking up too much of your time? Let's run through the most common problems that an IT administrator can encounter when dealing with Office 365 email signatures.
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
CodeTwo Sync for iCloud ( automatically synchronizes your Outlook 2016, 2013, 2010 or 2007 folders with iCloud folders available via iCloud Control Panel. This lets you automatically sync them with…

751 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question