Solved Automate task of copying file from Outlook to a server path

June 10, 2017 at 22:15:58
Specs: Windows 7
Hi... I got weekly basis a couple of emails with file attachment from a 3rd party in my MS outlook email and I then have to copy these 2 files in a specific server path where one of our job picked up the file and then they are processed. How can I automate that task of copying these files from my outlook to the server path. please share ideas so that I can implement.

message edited by rkptcs


See More: Automate task of copying file from Outlook to a server path

Reply ↓  Report •

#1
June 12, 2017 at 01:16:51
✔ Best Answer
Hi try this code.

To filter out the specific email that you want to extract the attachments from, you can specify the senders name and the subject line of the email, assuming they are always the same.

This way the code will monitor the incoming emails and extract the attachments from an email that fulfills those two criteria. If this is subject to change then perhaps omit this from the code.

The code will need to be pasted in the 'ThisOutlookSession' module.

Also you will need to restart Outlook once you have pasted the code in

You will be able to test this by sending yourself a test email with an attachment and see how it works, it works perfectly for me, saving my attachments to a server location.

Private WithEvents myOlItems  As Outlook.Items

Private Sub Application_Startup()
' Declare all your variables

    Dim olApp As Outlook.Application
    Dim objNS As Outlook.NameSpace
    Set olApp = Outlook.Application
    Set objNS = olApp.GetNamespace("MAPI")
    Set myOlItems = objNS.GetDefaultFolder(olFolderInbox).Items
End Sub

Private Sub myOlItems_ItemAdd(ByVal item As Object)

    On Error GoTo ErrorHandler

    Dim Msg As Outlook.MailItem

' Check the type of item Outlook just recieved, if it is an email then continie

    If TypeName(item) = "MailItem" Then
        Set Msg = item
        
' Here we can filter out specific emails depending on the sender and the subject, i am not sure
' if you recieve your emails from the same person or not and if the subject lines are consistent
' but this will be useful so that you dont save all attachments from every email you get

' You may have to ask your sender to send you a test email to determine how the sender name appears.
' You will see the sender name in the immediate window (View > Immediate Windows - Or press CTRL and G)
        
        Debug.Print Msg.Sender

        If Msg.Sender = "Lastname Firstname" And Msg.Subject = "Subject line goes here" Then
        
            For i = 1 To Msg.Attachments.Count

' Specify the path to where you want the attachments to be saved. Ensure you include a "\" at the end of the server path

                Msg.Attachments.item(i).SaveAsFile "\\Server Path\" & Msg.Attachments.item(i).FileName
            
            Next i
        
        End If
                
    End If

ProgramExit:
    Exit Sub
ErrorHandler:
    MsgBox Err.Number & " - " & Err.Description
    Resume ProgramExit
End Sub

message edited by AlwaysWillingToLearn


Reply ↓  Report •

#2
July 11, 2017 at 10:55:06
thanks @AlwaysWillingToLearn for helping out. i will try this and let you know the outcome.

Reply ↓  Report •
Related Solutions


Ask Question