Type Mismatch in Loop to scan Outlook Messages

前端 未结 1 1952
执笔经年
执笔经年 2020-12-11 19:21

I get an intermittent error when looping through the Outlook inbox using VBA. A type mismatch occurs on the Next objOutlookMesg line.

Note: I wanted to be as thour

相关标签:
1条回答
  • 2020-12-11 19:38

    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
    
    0 讨论(0)
提交回复
热议问题