Link to home
Start Free TrialLog in
Avatar of Mike French
Mike FrenchFlag for United States of America

asked on

Excel vba finding attachment in Outlook

I have code that runs through and finds an attachment in Outlook. It takes a long time due to the fact that I have a lot of emails in my inbox. The problem is that, for some reason, the search starts with the oldest emails in the inbox. Why is this? Is there a way to make the search begin with the newest emails?

Sub GetAttachmentFromInbox()

 Dim ns As Namespace
 Dim Inbox As MAPIFolder
 Dim Item As Object
 Dim Atmt As Attachment
 Dim FileName As String
 
 Set ns = GetNamespace("MAPI")
 Set Inbox = ns.GetDefaultFolder(olFolderInbox)
 
  For Each Item In Inbox.Items
    For Each Atmt In Item.Attachments
            If Left(Atmt.FileName, 11) = "JAN_-_Sales" Then
            Stop
            'FileName = Range("COFolder").Value & "\" & Atmt.FileName
            MsgBox FileName
            'Atmt.SaveAsFile FileName
            Exit Sub
        End If
    Next Atmt
 Next Item
 
End Sub
Avatar of Daniel Pineault
Daniel Pineault

Could you not add a restrict to your iteration to limit the items to actually process?
Avatar of Mike French

ASKER

How can I restrict it to today's date?
ASKER CERTIFIED SOLUTION
Avatar of Daniel Pineault
Daniel Pineault

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Thanks Daniel!!!

I should be able to make this code work!