Solved

Automatically Save New Attachments

Posted on 2004-04-19
14
602 Views
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?
0
Comment
Question by:Mourdekai
  • 7
  • 6
14 Comments
 
LVL 49

Accepted Solution

by:
sunray_2003 earned 288 total points
Comment Utility
0
 
LVL 8

Author Comment

by:Mourdekai
Comment Utility
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?
0
 
LVL 23

Expert Comment

by:slink9
Comment Utility
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
oMessage.Attachments.Item(1).SaveAsFile_
   "C:\reports\in\" & NbrMsgs & "report.txt"
                        .Delete
           End If
             End With
Next
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).
0
 
LVL 8

Author Comment

by:Mourdekai
Comment Utility
Is there some way I can set up a rule to run a script that will do this?
0
 
LVL 23

Expert Comment

by:slink9
Comment Utility
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.
0
 
LVL 8

Author Comment

by:Mourdekai
Comment Utility
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.
0
 
LVL 23

Expert Comment

by:slink9
Comment Utility
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!!
0
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
LVL 23

Expert Comment

by:slink9
Comment Utility
Continued ...
If you don't have backups each hard drive has to be sent off to a data recovery service like www.ontrack.com 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.
0
 
LVL 8

Author Comment

by:Mourdekai
Comment Utility
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.
0
 
LVL 23

Expert Comment

by:slink9
Comment Utility
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.

How?

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.
0
 
LVL 8

Author Comment

by:Mourdekai
Comment Utility
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.
0
 
LVL 23

Expert Comment

by:slink9
Comment Utility
Property SenderEmailAddress As String
    read-only
    Member of Outlook.MailItem
0
 
LVL 8

Author Comment

by:Mourdekai
Comment Utility
I don't have that property, I'm using Outlook 2002.
0
 
LVL 8

Author Comment

by:Mourdekai
Comment Utility
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:

http://www.winnetmag.com/Windows/Article/ArticleID/8630/8630.html
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

My experience with Windows 10 over a one year period and suggestions for smooth operation
This process describes the steps required to Import and Export data from and to .pst files using Exchange 2010. We can use these steps to export data from a user to a .pst file, import data back to the same or a different user, or even import data t…
This Experts Exchange video Micro Tutorial shows how to tell Microsoft Office that a word is NOT spelled correctly. Microsoft Office has a built-in, main dictionary that is shared by Office apps, including Excel, Outlook, PowerPoint, and Word. When …
To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…

728 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now