以下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.v1Confirmation.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
您正在对一个Items
集合进行排序,但最终会使用一个完全不同的对象 - 每次调用MAPIFolder.Items
时,您都会收回一个全新的COM对象,该对象不知道任何其他实例。读一次项目集合,将其存储在变量中,然后循环浏览其项目:
set myItems = myFolder.Items
myItems.Sort "[ReceivedTime]", True
For Each myItem In myItems
...
myitems.sort可以。
myfolder.items.sort,尽管您可能看到的文档可能无效。
Sub SaveAttachments_RsConfirmation()
Dim myOlapp As Outlook.Application
Dim myNameSpace As Outlook.NameSpace
Dim myFolder As Outlook.MAPIFolder
Dim myItems As Outlook.items
Set myOlapp = CreateObject("Outlook.Application")
Set myNameSpace = myOlapp.GetNamespace("MAPI")
Set myFolder = myNameSpace.GetDefaultFolder(olFolderInbox)
Set myFolder = myFolder.folders("Rs.Confirmation")
' Attempt to sort items in the folder
myFolder.items.Sort "[ReceivedTime]", True
Debug.Print myFolder.items(1).Subject
' False should be no different from True
myFolder.items.Sort "[ReceivedTime]", False
Debug.Print myFolder.items(1).Subject
' Create a collection of items
Set myItems = myFolder.items
myItems.Sort "[ReceivedTime]", True
Debug.Print myItems(1).Subject
' False should sort opposite to True
myItems.Sort "[ReceivedTime]", False
Debug.Print myItems(1).Subject
ExitRoutine:
Set myOlapp = Nothing
Set myNameSpace = Nothing
Set myFolder = Nothing
Set myItems = Nothing
End Sub
我看到您对文件夹进行排序,取每个项目,然后再次对整个文件夹进行排序。这可能不是您的意思。
myFolder.Items.Sort "[ReceivedTime]", True ' sort folder
For Each myItem In myFolder.Items ' process each item
myFolder.Items.Sort "[ReceivedTime]", True ' sort folder again: remove this line
If myItem.Attachments.Count <> 0 Then
For Each myAttachment In myItem.Attachments
I = 1 ' shouldn't this be incremented?
请注意,如果I
没有增加,则将所有附件放在同一文件中(替换),因此您只会看到一个附件,这将是文件夹中最后一封电子邮件中的一个附件。
我不确定您的问题是什么。首先,我将删除第二排序命令。重新排序可能会弄乱获得myItem
的顺序。
但是,如果您想进入每个子文件夹和子文件夹等,则必须开发一个递归过程,该过程对文件夹进行分类,处理每个项目,如果项目是文件夹,请下降到子文件夹中,排序folderr等。