【发布时间】:2022-03-18 02:16:37
【问题描述】:
下面的 Excel VBA 代码适用于一个子文件夹(提取最新附件),但当应用于另一个子文件夹时,它会从最旧的电子邮件中提取信息,而不是最新的。
myFolder.Items.sort 是正确的方法吗?
Sub SaveAttachments_RsConfirmation()
Dim myOlapp As Outlook.Application
Dim myNameSpace As Outlook.Namespace
Dim myFolder As Outlook.MAPIFolder
Dim myItem As Outlook.MailItem
Dim myAttachment As Outlook.Attachment
Dim I As Long
Set myOlapp = CreateObject("Outlook.Application")
Set myNameSpace = myOlapp.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myFolder = myFolder.Folders("Rs.Confirmation")
myFolder.Items.Sort "[ReceivedTime]", True
For Each myItem In myFolder.Items
myFolder.Items.Sort "[ReceivedTime]", True
If myItem.Attachments.Count <> 0 Then
For Each myAttachment In myItem.Attachments
I = 1
myAttachment.SaveAsFile "C:\Del.Gen.v1\Confirmation.Email\" & I & ".txt"
eSender = myItem.SenderEmailAddress
dtRecvd = myItem.ReceivedTime
dtSent = myItem.CreationTime
sSubj = myItem.Subject
sMsg = myItem.Body
Exit For
Next
End If
Next
Workbooks("Del.Gen.v1.xlsm").Worksheets("Sheet4").Range("A1").Value = eSender
Workbooks("Del.Gen.v1.xlsm").Worksheets("Sheet4").Range("A2").Value = dtRecvd
Workbooks("Del.Gen.v1.xlsm").Worksheets("Sheet4").Range("A3").Value = dtSent
Workbooks("Del.Gen.v1.xlsm").Worksheets("Sheet4").Range("A4").Value = sSubj
Workbooks("Del.Gen.v1.xlsm").Worksheets("Sheet4").Range("A5").Value = sMsg
Debug.Print eSender
Debug.Print dtRecvd
Debug.Print dtSent
Debug.Print sSubj
Debug.Print sMsg
End Sub
【问题讨论】: