【问题标题】:myItems.sort works for one Outlook subfolder but doesn't on othersmyItems.sort 适用于一个 Outlook 子文件夹,但不适用于其他子文件夹
【发布时间】: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

【问题讨论】:

    标签: excel vba outlook


    【解决方案1】:

    您正在对一个Items 集合进行排序,但最终使用了一个完全不同的对象- 每次调用MAPIFolder.Items 时,您都会返回一个不知道任何其他实例的全新COM 对象。读取 items 集合一次,将其存储在变量中,然后循环遍历其项目:

    set myItems = myFolder.Items
    myItems.Sort "[ReceivedTime]", True
    For Each myItem In myItems 
      ...
    

    【讨论】:

    • 感谢您的反馈德米特里。确实很有帮助!
    【解决方案2】:

    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
    

    【讨论】:

      【解决方案3】:

      我看到您对文件夹进行排序,取出每个项目并再次对整个文件夹进行排序。这可能不是你的意思。

      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 的顺序。

      但是如果你想进入每个子文件夹,子文件夹等等,你必须开发一个递归过程,对文件夹进行排序,处理每个项目,如果项目是一个文件夹,下降到子文件夹,排序文件夹等.

      【讨论】:

      • "请注意,如果 I 不递增,所有附件将被放置(替换)在同一个文件中,因此您只会看到一个附件,这将是文件夹中最后一封电子邮件中的附件。 " ----这很可能是我的问题,保罗,因为我尝试了很多变体并绕着圈子转,就像我在 vba 中自学时所经历的那样:)
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-04-12
      • 2014-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-23
      • 1970-01-01
      相关资源
      最近更新 更多