简体   繁体   中英

Moving read items from a folder back to inbox

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

在此处输入图片说明

Runtime Error I'm getting

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM