Check for the senderEmailAddress

Dim obj as a generic Objectthere are objects other than MailItem in your Inbox, also to improve your loop try using Items.Restrict Method (Outlook)

Option Explicit
Sub SetFlagIcon()
    Dim mpfInbox As Outlook.Folder
    Dim obj As Object
    Dim Items As Outlook.Items
    Dim i As Long
    Dim Filter As String
    
    Set mpfInbox = Application.GetNamespace("MAPI").GetDefaultFolder _
                                    (olFolderInbox).Folders("Temp")

    Filter = "[SenderEmailAddress] = '[email protected]'"
    
    Set Items = mpfInbox.Items.Restrict(Filter)
    
    ' Loop all items in the Inbox\Test Folder
    For i = 1 To Items.Count
        If Items(i).Class = olMail Then
            Set obj = Items(i)
            'Set the yellow flag icon
            obj.FlagIcon = olYellowFlagIcon
            obj.Save
        End If
    Next
    
End Sub

Items.Restrict Method Applies a filter to the Items collection, returning a new collection containing all of the items from the original that match the filter.

Leave a Comment