Solved

Save attachments and file email

Posted on 2006-10-27
6
2,727 Views
Last Modified: 2010-07-01
Hello All,

I would like to know anyone knows a function that will save the attachments of email with a specific subject ("Car Mileage Claim") to the windows directory ("C:\Car_Mileage_Attachments\"), and then move the email to an outlook folder ("Car_Mileage"). I would like this to run as the email comes in, but also when outlook opens to go through and check for any new emails.

Thanks, Dommie Lama
0
Comment
Question by:DomiLama
6 Comments
 
LVL 76

Expert Comment

by:David Lee
ID: 17818635
Hi DomiLama,

This is possible with the combination of a rule and a macro.  If using a macro is an option, then I can post the code and instructions for both.

Cheers!
0
 

Author Comment

by:DomiLama
ID: 17818736
Hi BlueDevilFan, yeah I think that might do the job. Can you send the code and I'll give it a go.

Thanks.
0
 
LVL 18

Expert Comment

by:Serena Hsi
ID: 17820101
Using VBA to Manage Your Outlook Email Attachments (tutorial and VBA walk-through)
http://www.fontstuff.com/outlook/oltut01.htm

Use the Rules wizard to trigger based on an email with an attachment, with "specific words" in the subject, perform a "custom action", and move the email to a "specified folder".
0
Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

 
LVL 76

Accepted Solution

by:
David Lee earned 500 total points
ID: 17827674
DomiLama,

The code for doing this is below.  Follow these instructions to use it.

1.  Start Outlook.
2.  Click Tools->Macro->Visual Basic Editor.
3.  If not already expanded, expand Modules and click on Module1.
4.  Copy the code below and paste it into the right-hand pane of the VB Editor.
5.  Edit the code as needed.  I placed comment lines where things need to change.
6.  Click the diskette icon on the toolbar to save the changes.
7.  Close the VB Editor.
8.  Click Tools->Macro->Security.
9.  Change the Security Level setting to Medium.
10.  Create a rule that runs when a new message arrives.  Set it to check the subject for the text you want to trigger on.  If found, set the rule to move the message to the "Car Mileage Claim" folder and to run the macro.  The order is immaterial.  The final rule should look something like this:

Apply this rule after the message arrives
with Car Mileage Claim in the subject
  and on this machine only
move it to the Car_Mileage folder
  and run Modules.SaveAttachmentsToDiskRule


Sub SaveAttachmentsToDiskRule(olkMessage As Outlook.MailItem)
    Dim olkAttachment As Outlook.Attachment, _
        objFSO As Object, _
        strRootFolderPath As String, _
        strFilename As String
    'Change the path on the following line to the folder you want the attachments save in
    strRootFolderPath = "C:\Car_Mileage_Attachments\"
    Set objFSO = CreateObject("Scripting.FileSystemObject")
    Set olkSourceFolder = Application.ActiveExplorer.CurrentFolder
    If olkMessage.Attachments.Count > 0 Then
        For Each olkAttachment In olkMessage.Attachments
            strFilename = olkAttachment.FileName
            intCount = 0
            Do While True
                If objFSO.FileExists(strRootFolderPath & strFilename) Then
                    intCount = intCount + 1
                    strFilename = "Copy (" & intCount & ") of " & olkAttachment.FileName
                Else
                    Exit Do
                End If
            Loop
            olkAttachment.SaveAsFile strRootFolderPath & strFilename
        Next
    End If
    Set objFSO = Nothing
    Set olkAttachment = Nothing
    Set olkMessage = Nothing
End Sub
0
 
LVL 1

Expert Comment

by:penlandt
ID: 24583120
I can't understand why - but this solution is not working for me.  I've tried.  The macro moves the message to the folder it should, but it does not save the attachments.  I tried putting a break point in the code so that I could step through it but it appears that macro never runs because the break point is never reached.  Any idea why?

Here's my rule:

Apply this rule after the message arrives
with New eBay Listing in the subject
  and on this machine only
move it to the Completed Messages folder
  and run Project1.SaveAttachmentsToDiskRule
  and mark it as read
Sub SaveAttachmentsToDiskRule(olkMessage As Outlook.MailItem)

    Dim olkAttachment As Outlook.Attachment, _

        objFSO As Object, _

        strRootFolderPath As String, _

        strFilename As String

    'Change the path on the following line to the folder you want the attachments save in

    strRootFolderPath = "C:\eBayListings\"

    Stop

    Set objFSO = CreateObject("Scripting.FileSystemObject")

    Set olkSourceFolder = Application.ActiveExplorer.CurrentFolder

    If olkMessage.Attachments.Count > 0 Then

        For Each olkAttachment In olkMessage.Attachments

            strFilename = olkAttachment.FileName

            intCount = 0

            Do While True

                If objFSO.FileExists(strRootFolderPath & strFilename) Then

                    intCount = intCount + 1

                    strFilename = "Copy (" & intCount & ") of " & olkAttachment.FileName

                Else

                    Exit Do

                End If

            Loop

            olkAttachment.SaveAsFile strRootFolderPath & strFilename

        Next

    End If

    Set objFSO = Nothing

    Set olkAttachment = Nothing

    Set olkMessage = Nothing

End Sub

Open in new window

0
 
LVL 1

Expert Comment

by:penlandt
ID: 24583666
Please disregard my previous post - I had forgotten to save the Project - that's why it wasn't working.  Rookie mistake ;-)
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
SBS 20011 to Office 365 7 86
Unable to find attach icon in MS outlook 2016 3 50
EXCH2013 - Archive mailboxes 2 42
Problem with Yahoo and Outlook SMTP Email 7 119
Outlook Free & Paid Tools
MS Outlook is a world-class email client application that is mainly used for e-communication globally.  In this article, we will discuss the basic idea about MS Outlook, its advanced features, and types of MS Outlook File formats.
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 (http://www.codetwo.com/sync-for-icloud?sts=6554) 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…

863 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

27 Experts available now in Live!

Get 1:1 Help Now