Solved

VBA to Save CSV Attachment to harddrive when email is received into Inbox

Posted on 2014-03-12
2
1,817 Views
Last Modified: 2014-03-12
I am working on an Outlook VBA procedure to save the CSV attachment of an email with a specific subject text when the email is received into the Inbox.  So far, I have a rule set to run a script when the email with the subject text is received.  What I am finding is that the rule identifies the email properly and fires the VBA script but the email has not actually entered the Inbox so the VBA runs to completion and nothing is saved, then the email appears in the Inbox.   If I run the VBA manually once the email is in the inbox it saves the CSV to the proper location.

In the same routine, I would like to move the email to a subfolder (“Executive_Dashboard” in this case) after the attachment has been saved to the network location.

We use Outlook as the client with Google g-Mail as the backend mail server.  We do not have an Exchange environment.   I am primarily an Excel person and only have a limited understanding of the Outlook object model so please be specific if you can.   Through research I have read about NewMailEx and AddItem as possible solution but I don’t fully understand the concepts.

My first attempt is below.   If there  is a better way I am more than willing to trash what I have and go with a better solution.

Many Thanks,
Jerry
Sub MoveDashboardAttachments(Item As Outlook.MailItem)

Dim ns As NameSpace
Dim Inbox As MAPIFolder
Dim Atmt As Attachment
Dim SubFolder As MAPIFolder
Dim FileName As String
Dim olCurrMailItem As MailItem
Dim d As Integer
Dim intUwv As Integer
Dim intWv As Integer
Dim i As Integer

Set ns = GetNamespace("MAPI")
Set Inbox = ns.GetDefaultFolder(olFolderInbox)
Set SubFolder = Inbox.Folders("Dashboard_Executive")

i = 0
For Each Item In Inbox.Items
    d = DateDiff("d", CDate(Item.ReceivedTime), CDate(Now()))
    If d <= 5 Then
        
        'identify which email is being processed by looking at the email-subject
        intUwv = InStr(1, Item.Subject, "Attachment Test", 1)
        intWv = InStr(1, Item.Subject, "Attachment Testing", 1)
        
        ' if one of the emails is identified then process the email attachments
        If intUwv + intWv > 0 Then
            For Each Atmt In Item.Attachments
                If Right(Atmt.FileName, 3) = "csv" Then
                    FileName = "\\FLFILE01\Warehouse\Data\Jerry\" & Atmt.FileName
                    Atmt.SaveAsFile FileName
                    Item.Move SubFolder
                    i = i + 1
                End If
            Next Atmt
            intUwv = 0
            intWv = 0
        End If
        
    End If
Next Item
        
Set Atmt = Nothing
Set Item = Nothing
Set ns = Nothing
Exit Sub
        
End Sub

Open in new window

0
Comment
Question by:Jerry Paladino
2 Comments
 
LVL 48

Accepted Solution

by:
Rgonzo1971 earned 500 total points
Comment Utility
Hi,

As script pls try

Public Sub saveCSVAttach(itm As Outlook.MailItem)
Dim objAtt As Outlook.Attachment
d = DateDiff("d", CDate(itm.ReceivedTime), CDate(Now()))

If d <= 5 Then
    intUwv = InStr(1, itm.Subject, "Attachment Test", 1)
    intWv = InStr(1, itm.Subject, "Attachment Testing", 1)
    If intUwv + intWv > 0 Then
        For Each objAtt In itm.Attachments
            If objAtt.FileName Like "*.csv" Then
                FileName = "\\FLFILE01\Warehouse\Data\Jerry\" & objAtt.FileName
                objAtt.SaveAsFile FileName
                Set objAtt = Nothing
            End If
        Next
    End If
End If

End Sub

Open in new window

Why don't you try to use the rule to filter by  the subject text and to move the mail to the desired folder

Regards
0
 
LVL 16

Author Closing Comment

by:Jerry Paladino
Comment Utility
Rgonzo1971:

Works perfect.   I appreciate your help.
Thank You,
Jerry
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Find out how to use dynamic social media in email signatures with this top 10 DOs & DON’Ts.
Use these top 10 tips to master the art of email signature design. Create an email signature design that will easily wow recipients, promote your brand and highlight your professionalism.
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…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

15 Experts available now in Live!

Get 1:1 Help Now