Outlook VBA How to loop through inbox and list from email email address if subject matches

前端 未结 1 730
旧巷少年郎
旧巷少年郎 2020-12-06 12:34

I\'m trying to use Outlook VBA to loop through the inbox and list the from email address if the subject matches a string. Got this so far from googling, but it\'s not workin

相关标签:
1条回答
  • 2020-12-06 13:13

    As commented, try incorporating 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 suggested by Dmitry, you can also use:

    If Item.Class = 43 Then
    

    in place of

    If TypeOf Item Is Outlook.MailItem Then
    
    0 讨论(0)
提交回复
热议问题