PADARO_150
PADARO_150

Reputation: 13

How to loop through items in a folder?

CONTEXT: I'm trying to first check if mails in a folder ("pending") are read. If the mail is read, move it to another folder ("done").
Then, for the remaining mails in "pending", save the attachments and mark the mail as "read".

PROBLEM: When I try to loop through the items in the folder it drops

Run-time error 438:
The object doesn't support this property or method

CODE:

Sub MoveInbox2Reviewed()

Dim OutlookApp As Outlook.Application
Dim ONameSpace As Object
Dim OItem As Outlook.MailItem
Dim OFolderSrc As Object
Dim OFolderDst As Object
Dim Path As String

Set OutlookApp = New Outlook.Application
Set ONameSpace = OutlookApp.GetNamespace("MAPI")

Set OFolderSrc = ONameSpace.GetDefaultFolder(olFolderInbox).Folders("pending")
Set OFolderDst = ONameSpace.GetDefaultFolder(olFolderInbox).Folders("done")

Path = "C:\Users\..."

For Each OItem In OFolderSrc
    If OItem.UnRead = False Then
        OItem.Move OFolderDst
    End If
Next

For Each OItem In OFolderSrc
    OItem.Attachments.SaveAsFile Path & Attachment.DisplayName
    OItem.UnRead = False
Next

End Sub

Upvotes: 0

Views: 139

Answers (1)

Dmitry Streblechenko
Dmitry Streblechenko

Reputation: 66235

Firstly, you are looping through the folders. not items. You need OFolderSrc.Items.

Secondly, you should never loop through all items in a folder, use Items.Restrict:

For Each OItem In OFolderSrc.Items.Restrict("[Unread] = true")

Upvotes: 1

Related Questions