Solved

Auto save an attachment in a designated directory

Posted on 2006-07-24
9
576 Views
Last Modified: 2010-08-05
I always receive the same attachment everyday at a certain time which I have to save to a particular directory. In doing so I overwrite the original document that I recived the day before. It is an updated file that we use for our intranet

Is there a way I can use a macro to auto save this attachment to the particular directory and overwriting the original?
Also bear in mind that both documents have diferent names. So in order to overwrite I always had to rename the received attachment to match the original in order for the link on the intranet to work. i.e. the original doc is called DocA.xls and the attachment comes in as DocB.xls, so I have to rename the attachement to DocA.xls and overwritting the original.

Is there a way to automate this process?
Thanks!
0
Comment
Question by:proltdman
  • 5
  • 3
9 Comments
 
LVL 13

Assisted Solution

by:stefri
stefri earned 62 total points
ID: 17170138
Which version of outlook do you use? If version is 2002 or 2003 and if the mail has always the same subject, you may give a try to this:
in Outlook

'Open VBA (Alt F11)
cut and paste the code below
'-------- start SNIP
' ----- Adjust the value of saveFolder to whatever you want -------
 ----- Adjust the value of theFile to whatever you want -------

Public Sub saveAttachtoDisk (itm As Outlook.MailItem)

Dim objAtt As Outlook.Attachment

Dim saveFolder As String, theFile as String
theFile="docB.doc"
saveFolder = "c:\temp\"

    For Each objAtt In itm.Attachments
        objAtt.SaveAsFile saveFolder & "\" & theFile
        Set objAtt = Nothing
    Next
End Sub
'-------------- end SNIP

1) Set Tools/Macros/Security to Medium
2)Close Outlook:  save the project when asked
3) Restart outlook: accept the Enable Macro Warning to get the macro fired then

4)Create a rule
4.1)check mail when they arrive
4.2)with specific words in the subject
4.3)run a script
4.4)select saveAttachtoDisk from the available macros
4.5)stop processing more rules
6)Enjoy
Stefri'
0
 

Author Comment

by:proltdman
ID: 17170266
Ok I wil give this a try and let you know. Thanks!
I use outlook 2003

0
 
LVL 76

Expert Comment

by:David Lee
ID: 17172059
Hi, stefri.

I'd recommend a minor change to your code.  It's possible that someone might send a message with same subject that the rule is watching for and the code would save any attachment that message had.  It's also possible that a legitimate message with the correct attachment and one or more additional attachments would arrive and the code would save them all, overwriting the file with the last attachment in line.  Both of these can be avoided by simply testing the attachment name to make sure it's the file proltdman is looking for.  Just a thought.
0
 

Author Comment

by:proltdman
ID: 17173665
Perhaps I should also mention that the attached file also has the day's date appended to it. i.e DocA 24.07.06.xls, hence it comes as a different file each day, but the SUBJECT field always has the same title.
I'm not sure if this will make it any easier to code.
0
Zoho SalesIQ

Hassle-free live chat software re-imagined for business growth. 2 users, always free.

 
LVL 76

Accepted Solution

by:
David Lee earned 63 total points
ID: 17191107
proltdman,

Here's the code I'd use to handle this.  It goes in a module, not in ThisOutlookSession.  Follow stefri's instructions for creating a rule to fire the script.  This version adds the checks I described earlier and also marks the message as read once it's processed.

Sub SaveFileToDisk(Item As Outlook.MailItem)
    'Make sure the message has just one attachment
    If Item.Attachments.Count = 1 Then
        'Make sure the one attachment's filename beings with DocB
        If Left(Item.Attachments.Item(1).FileName, 4) = "DocB" Then
            'Change the path on the next line
            Item.Attachments.Item(1).SaveAsFile "C:\eeTesting\DocA.xls"
            Item.UnRead = False
            Item.Save
        End If
    End If
End Sub
0
 

Author Comment

by:proltdman
ID: 17191778
Thanks BDF,
However, as I said earlier, the incoming attachment is date stamped by the sender to reflect the day of sending, hence DocB comes in as DocB 24.07.06 or DocB 25.07.06 etc etc etc!
Can I use a wild card format in the line
 If Left(Item.Attachments.Item(1).FileName, 4) = "DocB" Then
changed to
 If Left(Item.Attachments.Item(1).FileName, 4) = "DocB *" Then

or won't that work?
0
 
LVL 76

Expert Comment

by:David Lee
ID: 17197292
I guess I don't see what the problem is with the code as is.  It doesn't matter what comes after "DocB" since I'm only checking the leftmost four characters which will always be DocB.  There's no difference in checking the leftmost four characters and checking the same four characters and anything that follows, is there?  As to the syntax "DocB *" that won't work with the Left function.  It doesn't understand wildcards.
0
 

Author Comment

by:proltdman
ID: 17199557
Sorry i didn't click on the "Left" function you implemented, I will try it now..
0
 

Author Comment

by:proltdman
ID: 17221952
Great, it works!

I used the code from BlueDevilFan and set up the rules as suggested by stefri.

Thanks for your efforts guys, I really appareciate it.

I will split the points between you two. I know it's not much but it just shows that I value both of your efforts.

proltdman.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
ost file to pst 10 102
Outlook 2013 has lost the address book or contacts list 8 16
What is the IP address of my host outgoing email servers? 13 40
exchange, outlook 7 38
If you don't know how to downgrade, my instructions below should be helpful.
Read this checklist to learn more about the 15 things you should never include in an email signature.
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…

919 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

18 Experts available now in Live!

Get 1:1 Help Now