You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

从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, use olFolderDeletedItems, and for the Inbox, use olFolderInbox.
  • Folder Variable Mix-Up: You set myInbox to 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 myItems collection, the collection itself changes, which can cause FindNext to skip items or throw errors. Using Restrict is 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?

  1. Clearer Variable Names: Renamed variables like myDeletedFolder instead of misusing myInbox to avoid confusion.
  2. Proper Constant References: Used olFolderDeletedItems and olFolderInbox to correctly target the default folders.
  3. 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.
  4. 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.
  5. 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 SenderName isn'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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.28 09:47:55