Home > Blockchain >  MailItem.Move method in Outlook VBA not working
MailItem.Move method in Outlook VBA not working

Time:03-15

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 move each read mail from the "pending" folder to "done" with ".Move", drops

"error 424. An object is required".

I think the problem is here OItem.Move OFolderDst. I can't find why is it wrong.

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.Items.Restrict("[Unread] = False")
    OItem.Move OFolderDst 'The problem is here
Next

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

End Sub

CodePudding user response:

Each time the Move call is made the collection is decreased by one item. So, I'd recommend using the reverse for loop instead for moving items from a folder. For example:

for i = Items.Count to 1 step -1
  Items(i).Move folder
next

So, in the code you may get all unread items (represented by the Items collection) and then call a reverse loop to move items.

  • Related