Iterate all email items in a specific Outlook folder

前端 未结 3 563
梦如初夏
梦如初夏 2020-12-09 13:10

How can I in an Outlook VBA macro iterate all email items in a specific Outlook folder (in this case the folder belongs not to my personal inbux but is a sub-folder to the i

相关标签:
3条回答
  • 2020-12-09 13:47

    In my case the following worked:

    Sub ListMailsInFolder()
    
        Dim objNS As Outlook.NameSpace
        Dim objFolder As Outlook.MAPIFolder
    
        Set objNS = GetNamespace("MAPI")
        Set objFolder = objNS.Folders.GetFirst ' folders of your current account
        Set objFolder = objFolder.Folders("Foldername").Folders("Subfoldername")
    
        For Each Item In objFolder.Items
            If TypeName(Item) = "MailItem" Then
                ' ... do stuff here ...
                Debug.Print Item.ConversationTopic
            End If
        Next
    
    End Sub
    

    Likewise, you can as well iterate through calender items:

    Private Sub ListCalendarItems()
            Set olApp = CreateObject("Outlook.Application")
            Set olNS = olApp.GetNamespace("MAPI")
    
            Set olRecItems = olNS.GetDefaultFolder(olFolderTasks)
            strFilter = "[DueDate] > '1/15/2009'"
            Set olFilterRecItems = olRecItems.Items.Restrict(strFilter)
            For Each Item In olFilterRecItems
            If TypeName(Item) = "TaskItem" Then
                Debug.Print Item.ConversationTopic
            End If
        Next
    End Sub
    

    Note that this example is using filtering and also .GetDefaultFolder(olFolderTasks) to get the builtin folder for calendar items. If you want to access the inbox, for example, use olFolderInbox.

    0 讨论(0)
  • 2020-12-09 13:48
    Sub TheSub()
    
    Dim objNS As Outlook.NameSpace
    Dim fldrImAfter As Outlook.Folder
    Dim Message As Outlook.MailItem
    
        'This gets a handle on your mailbox
        Set objNS = GetNamespace("MAPI")
    
        'Calls fldrGetFolder function to return desired folder object
        Set fldrImAfter = fldrGetFolder("Folder Name Here", objNS.Folders)
    
        For Each Message In fldrImAfter.Items
            MsgBox Message.Subject
        Next
    
    End Sub
    

    Recursive function to loop over all folders until the specified folder name is found....

    Function fldrGetFolder( _
                        strFolderName As String _
                        , objParentFolderCollection As Outlook.Folders _
                        ) As Outlook.Folder
    
    Dim fldrSubFolder As Outlook.Folder
    
        For Each fldrGetFolder In objParentFolderCollection
    
            'MsgBox fldrGetFolder.Name
    
            If fldrGetFolder.Name = strFolderName Then
                Exit For
            End If
    
            If fldrGetFolder.Folders.Count > 0 Then
                Set fldrSubFolder = fldrGetFolder(strFolderName, 
    fldrGetFolder.Folders)
                If Not fldrSubFolder Is Nothing Then
                    Set fldrGetFolder = fldrSubFolder
                    Exit For
                End If
            End If
    
        Next
    
    End Function
    
    0 讨论(0)
  • 2020-12-09 13:50

    The format is:

    Set objFolder = objFolder.Folders("Inbox").Folders("mySubFolder1").Folders("mySubFolder2")
    

    As advised in a comment "move the next item line to before the ProgramExit label"

    0 讨论(0)
提交回复
热议问题