VBA to Save file from Outlook

Rpaikh

New Member
Joined
Jul 28, 2019
Messages
27
Hello Experts,


I'm looking for a VBA to save files from specific folder in Outlook and found below code from this forum and it works just fine.

However,

- The code just save only the first file of the first email >>> I would like to save all file for all unread email
- It always save and combine under .XLSX format >>> How can I save those file as original name & original format type
>>> How can I set a criteria to save only specific format i.e. only save .xlsx files out of all the file in that email




VBA Code:
Sub do_something_cool()



'Create the file path where I want the file saved
    Dim AttachementPath As String: Dim NewFileName As String: Dim NewFileName2 As String
        AttachementPath = "C:\Users\paikhr\Desktop\test\"  'think about creating environ$ or make this folder if not there
        NewFileName = AttachementPath & "Daily Reporting File " & Format(Date, "MM-DD_YY") & ".xlsx"
        


Dim olApp As Outlook.Application
Dim ns As Outlook.Namespace
Dim Inbox_Prog_Pace As MAPIFolder


Dim Item As Object
Dim PaceFile As Attachment


    Set ns = GetNamespace("MAPI")
    Set Inbox_Prog_Pace = ns.GetDefaultFolder(olFolderInbox).Folders("Milot").Folders("GR PK RM")
    
'Check the pacing file folder for an unread email
'If there are no undread emails then display a message
    If Inbox_Prog_Pace.Items.Restrict("[UnRead] = True").Count = 0 Then
        MsgBox "The pacing file has already been read and will not be downloaded"
        Exit Sub
    End If


'Get the attachement from the 1st unread email
    For Each Item In Inbox_Prog_Pace.Items.Restrict("[Unread] = True")
        'checking if there is actually an attachment
        If Item.Attachments.Count <> 0 Then
            For Each PaceFile In Item.Attachments
                PaceFile.SaveAsFile NewFileName 'need to save as .csv on order to open the file out of the folder - other types or blank do not work
            Exit For
        Next
        Else
            MsgBox "There is not an attachment in the email, please QA"
        End If
        
        Item.UnRead = False
        DoEvents
        Item.Save
        
        Exit For
    Next
    
End Sub
 

Excel Facts

Best way to learn Power Query?
Read M is for (Data) Monkey book by Ken Puls and Miguel Escobar. It is the complete guide to Power Query.

Forum statistics

Threads
1,224,827
Messages
6,181,197
Members
453,021
Latest member
pingpong7117

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top