Type Mismatch in Loop to scan Outlook Messages [duplicate]

An outlook folder has a default object type (MailItem, AppointmentItem, ContactItem, etc) but can actually hold any item type. So you’re hitting an item that’s not a MailItem and, by virtue of a For Each loop, trying to assign it to a variable that is a MailItem type.

You need to loop through with a generic Object and test the TypeName.

Dim oItem As Object
Dim oMail As MailItem

For Each oItem In Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
    If TypeName(oItem) = "MailItem" Then
        Set oMail = oItem

        'do stuff with omail
    End If
Next oItem

Leave a Comment