CONTEXT: I'm trying to build an Outlook macro that firstly checks if all the mails in a specific folder ("pending") are read or not. If the mail is read, the macro moves it to another specific folder ("done").
Then, for the resting mails in "pending", the macro saves the attachments of each mail and marks them as "read".
PROBLEM: When I try to check if the mail is read or not with "UnRead", drops error 438: The object does not admit this property or method.
Thanks in advance!
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
CodePudding user response:
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")