0

I am trying to mark an email with a specific subject as read and move it to another folder. The error is on the line .UnRead = False.

This line is causing the error 438. I have tried different combinations of myitem with the same result.

    Dim OutlookApp As Outlook.Application
    Dim OutlookNamespace As Namespace
    Dim Folder As Outlook.MAPIFolder
    Dim OutlookMail As Variant
    Dim DestFolder As Outlook.MAPIFolder
    Dim RetiredFolder As Outlook.MAPIFolder
    Dim myItem As Object
    Dim myItems As Outlook.MailItem


    Application.ScreenUpdating = False

    Set wb = ThisWorkbook
    Set ws = wb.Worksheets("Emails")
    Set wsNS = wb.Worksheets("NS_Export")
    ws.Activate
    Set OutlookApp = New Outlook.Application
    Set OutlookNamespace = OutlookApp.GetNamespace("MAPI")
    Set Folder = OutlookNamespace.Folders("myemail").Folders("NOC Announcements New")
    Set RetiredFolder = OutlookNamespace.Folders("tmyemail").Folders("Retired Buildings")

    For Each OutlookMail In Folder.Items

            Set myItem = Folder.Items.Find("[Subject] = 'The following building has been permanently retired'")
                            With myItem
                            .UnRead = False
                            .Move RetiredFolder
                        End With

        Next OutlookMail
0m3r
  • 12,286
  • 15
  • 35
  • 71
user2576682
  • 121
  • 2
  • 2
  • 10

1 Answers1

0

Use this :

Dim outlookmail As Outlook.MailItem
    For Each outlookmail In Folder.Items
        If TypeOf outlookmail Is MailItem Then
        If outlookmail.Subject = "The following building has been permanently retired" Then
        With outlookmail
            .UnRead = False
            .Move RetiredFolder
        End With
        End If
        End If
    Next outlookmail
Shivang Gupta
  • 347
  • 4
  • 10