Outlook VBA How to iterate inbox and list from email address if match

I am trying to use Outlook VBA to go through the inbox and list the email address if the subject matches the line. Got it so far from googling, but it does not work:

Dim objNS As Outlook.NameSpace Set objNS = GetNamespace("MAPI") Set Items = objNS.GetDefaultFolder(olFolderInbox).Items Dim oFolder As Outlook.MAPIFolder Dim oMail As Outlook.MailItem For Each oMail In Items Debug.Print oMail.SenderEmailAddress Next 

Does anyone know why I get a type mismatch error on startup?

+7
vba outlook
source share
1 answer

As commented, try including a test for MailItem in your code:

 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 

Edit1: As Dmitry suggested, you can also use:

 If Item.Class = 43 Then 

instead

 If TypeOf Item Is Outlook.MailItem Then 
+12
source share

All Articles