Need to find most recent email matching criteria

vb6 , outlook Dallas, United States
  • 9 years ago
    This code finds emails with specific attahcments. I get this email once a day and I need this code to grab thje attachments and save them as directed...but I only want the most recent email that meets the criteria. Function CallsMove() ' This Outlook macro checks a the Outlook Inbox for messages ' with attached files (of any type) and saves them to disk. ' NOTE: make sure the specified save folder exists before ' running the macro. On Error GoTo GetAttachments_err ' Declare variables Dim myolApp As New Outlook.Application Dim ns As NameSpace Dim Inbox As MAPIFolder Dim Item As Object Dim Atmt As Attachment Dim FileName As String Dim I As Integer Set myolApp = CreateObject("Outlook.Application") Set ns = GetNamespace("MAPI") Set Inbox = ns.GetDefaultFolder(olFolderInbox) I = 0 ' Check Inbox for messages and exit of none found If Inbox.Items.Count = 0 Then MsgBox "There are no messages in the Inbox.", vbInformation, _ "Nothing Found" Exit Function End If ' Check each message for attachments For Each Item In Inbox.Items ' Save any attachments found For Each Atmt In Item.Attachments ' This path must exist! Change folder name as necessary. If Atmt.FileName Like "Call List by*" Then Atmt.SaveAsFile "P:\databases\downloads\extensionstats\Reps\" & Atmt.FileName I = I + 1 End If Next Atmt If I = 1 Then Exit For End If Next Item ' Clear memory GetAttachments_exit: Set Atmt = Nothing Set Item = Nothing Set ns = Nothing Exit Function ' Handle errors GetAttachments_err: MsgBox "An unexpected error has occurred." _ & vbCrLf & "Please note and report the following information." _ & vbCrLf & "Macro Name: GetAttachments" _ & vbCrLf & "Error Number: " & Err.Number _ & vbCrLf & "Error Description: " & Err.Description _ , vbCritical, "Error!" Resume GetAttachments_exit End Function

Post a reply

No one has replied yet! Why not be the first?

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Perfection is achieved, not when there is nothing more to add, but when there is nothing left to take away.” - Antoine de Saint Exupéry