我创建了一个文件夹,根据规则存放所有邮件。文件夹中的邮件每天都在不断累积。我想从该文件夹中的最新邮件下载附件。目前,我能够解析所有文件并从所有邮件下载附件。如何仅从最新邮件下载?以下是我的代码。
Sub FebAttachment_Click()
Const AttachmentPath As String = "D:\Documents and Settings\rahul.baskaran\Desktop\"
Dim oApp As Object, ONS As Object, OInb As Object
Dim OItem, OAtch As Object
Dim OFind As Object
Dim OMail As Object
Dim strName As String
Dim strExt As String
Set oApp = GetObject(, "Outlook.application")
Set ONS = oApp.GetNamespace("MAPI")
Set OInb = ONS.Folders("Archive Folders").Folders("BIZOPS").Folders("2014.02")
Set OMail = OInb.Items
For Each OItem In OInb.Items
If OItem.Attachments.Count <> 0 Then
For Each OAtch In OItem.Attachments
strName = OAtch.Filename
strExt = Split(strName, ".z")(0)
OAtch.SaveAsFile AttachmentPath & OAtch.Filename
Exit For
Next
Else
MsgBox "The mail doesn't have an attachment"
End If
Next OItem
答案 0 :(得分:1)
按创建日期(Items.Sort)按降序对项目进行排序,然后检索集合中的第一个项目。
确保您的代码在相同的Items集合上运行(检索OInb.Items一次并将其缓存在变量中)。