none
problem looping all mails in all folders RRS feed

  • Question

  • Hi,I want to loop all mails in all folders. The following code is okay in VBA, but it does not work in VS2010.

    Please help.Thanks in advance.

    Public Sub ListAllMail()
        Dim olApp As New Outlook.Application
        Dim objFolder As Outlook.Folder
        Dim nmsName As Outlook.NameSpace
        Set nmsName = olApp.GetNamespace("MAPI")
        Dim i As Integer
       
        For Each f In nmsName.folders
            i = 0
            For Each itm In f.Items
            If i > 3 Then Exit For
                MsgBox f.Name & Chr(13) & itm.Subject
                i = i + 1
            Next
        Next
    
    End Sub

    Thursday, January 19, 2017 8:56 AM

Answers

  • Hello Leon,

    You need to define the item as object in the code and check the underlying type of the item as the code shows:

    Dim objNS As Outlook.NameSpace: Set objNS = GetNamespace("MAPI")
    Dim olFolder As Outlook.MAPIFolder
    Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
    Dim Item As Object
    
    For Each Item In olFolder.Items
        If TypeOf Item Is Outlook.MailItem Then 
            Dim oMail As Outlook.MailItem: Set oMail = Item
            Debug.Print oMail.SenderEmailAddress
        End If
    Next

    Also if required you may consider using Reflection for calling methods and properties dynamically without casting objects to the underlying type. See Type.InvokeMember for more information.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers


    • Edited by Eugene Astafiev Thursday, January 19, 2017 2:39 PM
    • Marked as answer by leon1526 Friday, January 20, 2017 1:26 AM
    Thursday, January 19, 2017 2:38 PM

All replies

  • Not every item in every folder is a MailItem.  You need to check the type of the item before you cast.
    Thursday, January 19, 2017 1:43 PM
  • Hello Leon,

    You need to define the item as object in the code and check the underlying type of the item as the code shows:

    Dim objNS As Outlook.NameSpace: Set objNS = GetNamespace("MAPI")
    Dim olFolder As Outlook.MAPIFolder
    Set olFolder = objNS.GetDefaultFolder(olFolderInbox)
    Dim Item As Object
    
    For Each Item In olFolder.Items
        If TypeOf Item Is Outlook.MailItem Then 
            Dim oMail As Outlook.MailItem: Set oMail = Item
            Debug.Print oMail.SenderEmailAddress
        End If
    Next

    Also if required you may consider using Reflection for calling methods and properties dynamically without casting objects to the underlying type. See Type.InvokeMember for more information.


    profile for Eugene Astafiev at Stack Overflow, Q&A for professional and enthusiast programmers


    • Edited by Eugene Astafiev Thursday, January 19, 2017 2:39 PM
    • Marked as answer by leon1526 Friday, January 20, 2017 1:26 AM
    Thursday, January 19, 2017 2:38 PM
  • Although I didn't quite understand it now, it was successful. Thank you Eugene!

    Friday, January 20, 2017 1:29 AM