微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

汇总所有电子邮件收件箱 + 子文件夹中的项目

如何解决汇总所有电子邮件收件箱 + 子文件夹中的项目

下午好,

我正在通过 Table 对象使用来自收件箱 + 子文件夹的所有电子邮件填充列表框。这工作正常。

然后,通过来自 DoubleclickListBox1 事件,我试图打开被选中的电子邮件。如果循环只通过收件箱文件夹,它是正确的。但是当我尝试从收件箱循环遍历子文件夹时,它不会。因此,我试图将收件箱 + 子文件夹中的所有电子邮件合二为一:

Set InBoxItems = SubFolder.Items

但除此之外它不起作用。可以做什么?

我的代码

Option Explicit

    Private Sub ListBox1_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
    
        Dim objNS As Outlook.namespace: Set objNS = GetNamespace("MAPI")
        Dim oFolder As Outlook.MAPIFolder: Set oFolder = objNS.GetDefaultFolder(olFolderInBox)
    
    
        Dim i As Long
        Dim j As Long
        Dim InBoxItems As Outlook.Items
        
        Dim thisEmail As Outlook.MailItem
        Dim SubFolder As Outlook.MAPIFolder
        Dim myArray() As String
        
        
        
        Dim Folders         As New Collection
        Dim entryID         As New Collection
        Dim StoreID         As New Collection
    
    
        Call GetFolder(Folders,entryID,StoreID,oFolder)
        myArray = ConvertToArray(indexEmailInBox)
        
        For j = 1 To Folders.Count
            Set SubFolder = Application.Session.GetFolderFromID(entryID(j),StoreID(j))
            Set InBoxItems = SubFolder.Items
        Next
        
         
    
    
            For i = LBound(myArray) To UBound(myArray)
                If Me.ListBox1.Selected(i) = True Then
                    If TypeName(InBoxItems.Item(onlyDigits(myArray(i)))) = "MailItem" Then ' it's an email
    
                        'MsgBox onlyDigits(myArray(UBound(myArray) - i - 1))
                        Set thisEmail = InBoxItems.Item(onlyDigits(myArray(UBound(myArray) - i - 1)))
                        Unload Me
                        thisEmail.display
                        Exit Sub
                    End If
                End If
            Next i
        
    
    End Sub


Function ConvertToArray(ByVal value As String)
    value = StrConv(value,vbUnicode)
    ConvertToArray = Split(Left(value,Len(value) - 1),"§")
End Function

Sub GetFolder(folders As Collection,entryID As Collection,StoreID As Collection,fld As MAPIFolder)

Dim SubFolder       As MAPIFolder

    folders.Add fld.FolderPath
    entryID.Add fld.entryID
    StoreID.Add fld.StoreID
    For Each SubFolder In fld.folders
        GetFolder folders,SubFolder
    Next SubFolder
    
ExitSub:

    Set SubFolder = nothing

End Sub

解决方法

您可以一次向一个集合中.Add 项。

Option Explicit ' Consider this mandatory
' Tools | Options | Editor tab
' Require Variable Declaration
' If desperate declare as Variant

Private Sub collection_Emails_Folder_And_Subfolders()
    
    Dim objFolder As folder
    Dim myItemsCol As New Collection
    Dim i As Long
    
    Dim myItems As Items
    
    Set objFolder = Session.PickFolder
    
    If objFolder Is Nothing Then
        Exit Sub
    End If
    
    'Set objFolder = Session.GetDefaultFolder(olFolderInbox)
    
    processFolder objFolder,myItemsCol
    
    ' Methods available are limited to:
    '  Add,Count,Item and Remove
    Debug.Print vbCr & "Final total - myItemsCol.Count: " & myItemsCol.Count
    
    ' You may access item properties
    For i = 1 To myItemsCol.Count
        Debug.Print " " & i & ": " & myItemsCol(i).ReceivedTime,myItemsCol(i).subject
    Next i
    
End Sub


Private Sub processFolder(ByVal objFolder As folder,ByVal myItemsCol As Collection)

    ' https://stackoverflow.com/questions/2272361/can-i-iterate-through-all-outlook-emails-in-a-folder-including-sub-folders

    Dim EmailCount As Long
    
    Dim myItem As Object
    Dim myItems As Items
    
    Dim i As Long
    
    Dim oFolder As folder
    
    Debug.Print vbCr & "objFolder: " & objFolder
    
    EmailCount = objFolder.Items.Count
    Debug.Print " EmailCount...: " & EmailCount
    
    If EmailCount > 0 Then
        
        Set myItems = objFolder.Items
        myItems.Sort "[ReceivedTime]",False ' oldest to newest
        
        For i = 1 To myItems.Count
            'Debug.Print " " & i & ": " & myItems(i).ReceivedTime,myItems(i).subject
            myItemsCol.Add myItems(i)
        Next
                
    End If
    
    Debug.Print " Running total: " & myItemsCol.Count
    
    If (objFolder.Folders.Count > 0) Then
        For Each oFolder In objFolder.Folders
            processFolder oFolder,myItemsCol
        Next
    End If
        
End Sub

您应该可以将 InboxItems 替换为 myItemsCol

If TypeName(myItemsCol.Item((onlyDigits(myArray(i)))) = "MailItem" Then ' it's an email

Set thisEmail = myItemsCol.Item(onlyDigits(myArray(UBound(myArray) - i - 1)))
,

似乎您只需要遍历 Outlook 中的所有子文件夹即可获取每个文件夹的项目数。

Sub Test()
 Set objOutlook = CreateObject( "Outlook.Application" )
 Set objNamespace = objOutlook.GetNamespace( "MAPI" )
 Set folders = objNamespace.DefaultStore.GetRootFolder().Folders

 EnumFolders folders
End Sub

Dim counter as Long = 0
' recursively invoked function
Sub EnumFolders(folders)
    For Each folder In folders

        Debug.Print folder.FolderPath

        Debug.Print folder.Count
        
        counter = counter + folder.Items.Count

        EnumFolders folder.Folders
    Next
End Sub

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。