Save email attachment macro - Only the last email

thenewworld

New Member
Joined
Mar 4, 2021
Messages
1
Office Version
  1. 365
Platform
  1. Windows
Hi guys

I've ran into some problems with my macro. Originally it's used to save daily excel-attachments from outlook locally. The macro search for the matching subject in my inbox and downloads it. Afterwards i move the email to a archive-folder in outlook, rinse and repeat.

We now want to send out these reports hourly and it's too much work to move the emails every hour so i have made a rule that moves the reports to a specific folder. Problem arises when my macro search in this folder it probably gets multiple hits and just keeps on running, finally saving the oldest attachment. Can my macro be tweaked so it saves only the latest attachment that i have received.

Macro as of today:

VBA Code:
Sub ImportEmail()
    
Dim fso As Object
Set fso = CreateObject("Scripting.FileSystemObject")

    Dim oOlAp As Object, oOlns As Object, oOlInb As Object
    Dim oOlItm As Object, oOlAtch As Object
    Dim A As String, B As String, C As String, D As String, E As String, F As String, G As String, H As String
      
    
    A = Sheets("Sheet1").Range("E2").Value
    B = Sheets("Sheet1").Range("E3").Value
    C = Sheets("Sheet1").Range("E4").Value
    D = Sheets("Sheet1").Range("E5").Value
    F = Sheets("Sheet1").Range("E6").Value
    G = Sheets("sheet1").Range("E7").Value
    
    H = Sheets("sheet1").Range("E8").Value
    
    E = Sheets("Sheet1").Range("J2").Value
    
    Set oOlAp = CreateObject("Outlook.Application")
    Set oOlns = oOlAp.GetNamespace("MAPI")
        
    Set oOlInb = oOlns.Folders("[I]Shared mailbox[/I]")
    Set oOlInb = oOlInb.Folders([I]"archive-folde[/I]r")
    For Each oOlItm In oOlInb.Items

    If oOlItm.Subject Like "*" & F & "*" Then
            For Each oOlAtch In oOlItm.Attachments
            oOlAtch.SaveAsFile E & F & ".xlsx"
            Exit For
            Next
    ElseIf oOlItm.Subject Like "*" & C & "*" And C <> "" Then
            For Each oOlAtch In oOlItm.Attachments
            oOlAtch.SaveAsFile E & C & ".xlsx"
            Exit For
            Next
    ElseIf oOlItm.Subject Like "*" & D & "*" And D <> "" Then
            For Each oOlAtch In oOlItm.Attachments
            oOlAtch.SaveAsFile E & D & ".xlsx"
            Exit For
            Next


    End If
    Next

End Sub
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.

Forum statistics

Threads
1,223,964
Messages
6,175,659
Members
452,666
Latest member
AllexDee

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