从Outlook已删除文件夹恢复邮件的VBA代码报错求助
Fixing Your Outlook VBA Move Items Error
Let's walk through the issues in your code step by step and get it working properly:
Key Issues in the Original Code
- Incorrect Constant Usage: You tried to pass the folder name as a string to
GetDefaultFolder, but this method requires an Outlook built-in constant. For the Deleted Items folder, useolFolderDeletedItems, and for the Inbox, useolFolderInbox. - Folder Variable Mix-Up: You set
myInboxto point to the Deleted Items folder, then tried to find the Inbox as a subfolder of it—this is wrong. The Inbox is a top-level default folder, not a child of Deleted Items. - Potential Traversal Issues: When you move items from the
myItemscollection, the collection itself changes, which can causeFindNextto skip items or throw errors. UsingRestrictis more reliable for filtering and traversing items.
Corrected VBA Code
Sub MoveItems() Dim myNameSpace As Outlook.NameSpace Dim myDeletedFolder As Outlook.Folder Dim myInbox As Outlook.Folder Dim filteredItems As Outlook.Items Dim currentItem As Object Dim i As Integer ' For reverse looping to avoid collection changes issues Set myNameSpace = Application.GetNamespace("MAPI") ' Correctly reference default folders using constants Set myDeletedFolder = myNameSpace.GetDefaultFolder(olFolderDeletedItems) Set myInbox = myNameSpace.GetDefaultFolder(olFolderInbox) ' Use Restrict to filter items by SenderName (more reliable than Find/FindNext) Set filteredItems = myDeletedFolder.Items.Restrict("[SenderName] = 'John Smith'") ' Loop backwards through filtered items to avoid issues when modifying the collection For i = filteredItems.Count To 1 Step -1 Set currentItem = filteredItems(i) ' Check if it's a mail item or calendar item before moving If TypeName(currentItem) = "MailItem" Or TypeName(currentItem) = "AppointmentItem" Then currentItem.Move myInbox End If Next i ' Clean up object references Set myNameSpace = Nothing Set myDeletedFolder = Nothing Set myInbox = Nothing Set filteredItems = Nothing Set currentItem = Nothing End Sub
What Changed?
- Clearer Variable Names: Renamed variables like
myDeletedFolderinstead of misusingmyInboxto avoid confusion. - Proper Constant References: Used
olFolderDeletedItemsandolFolderInboxto correctly target the default folders. - Restrict Method: This creates a static filtered collection of items matching your criteria, so you don't run into issues when items are moved out of the original folder.
- Reverse Loop: When modifying (moving) items in a collection, looping from the last item to the first prevents index shifting issues that can cause skipped items.
- Type Check: Added a check to ensure we're only moving mail or calendar items, which aligns with your goal of restoring emails and calendar events.
Additional Notes
- If
SenderNameisn't matching exactly, you might want to use[SenderEmailAddress]instead for more precise filtering. - Ensure you have the Outlook object library referenced in your VBA editor (go to Tools > References and check "Microsoft Outlook XX.X Object Library").
内容的提问来源于stack exchange,提问作者tackdetack
相关产品推荐
相关产品推荐

