Reputation: 43
I'm new to VBA and looking for a way to move read emails from a folder called "ForApproval" back into Inbox. Found this code on Stack and it works brilliantly, but when I try to reverse source and destination and put my folder name in - I'm getting: Run-time error '424': Object Required (see below screenshot)
Can someone have a quick look and say what's wrong in here?
Original Code:
Sub ReadMailMover()
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
**Set objFolderSrc = objNamespace.GetDefaultFolder(olFolderInbox)**
**Set objFolderDst = objFolderSrc.Parent.folders("__Reviewed")**
Set colitems = objFolderSrc.Items
Set colfiltereditems = colitems.Restrict("[UnRead] = False")
For intMessage = colfiltereditems.Count To 1 Step -1
colfiltereditems(intMessage).Move objFolderDst
Next
End Sub
My reversed version:
Sub ReadMailMover()
Set objOutlook = CreateObject("Outlook.Application")
Set objNamespace = objOutlook.GetNamespace("MAPI")
**Set objFolderSrc = objFolderSrc.Parent.Folders("ForApproval")**
**Set objFolderDst = objNamespace.GetDefaultFolder(olFolderInbox)**
Set colitems = objFolderSrc.Items
Set colfiltereditems = colitems.Restrict("[UnRead] = False")
For intMessage = colfiltereditems.Count To 1 Step -1
colfiltereditems(intMessage).Move objFolderDst
Next
End Sub
Upvotes: 3
Views: 172
Reputation: 5030
In the reversed code, when you initialise your variable objFolderSrc it includes a reference to itself.
Set objFolderSrc = objFolderSrc.Parent.Folders("ForApproval")
At this point objFolderSrc is uninitialized. It doesn't reference anything. For this reason it cannot be used to define a location.
Try this instead:
Set objFolderSrc = application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Parent.Folders("ForApproval")
Here the circular reference has been replaced. You source folder is referenced via the inbox.
Upvotes: 2