背景關系:我正在嘗試構建一個 Outlook 宏,它首先檢查是否已讀取特定檔案夾(“待處理”)中的所有郵件。如果郵件被讀取,宏會將其移動到另一個特定檔案夾(“完成”)。
然后,對于“待處理”中的其余郵件,宏保存每封郵件的附件并將它們標記為“已讀”。
問題:當我嘗試使用“.Move”將每個已讀郵件從“待處理”檔案夾移動到“完成”時,會掉落
“錯誤 424。需要一個物件”。
我認為問題就在這里OItem.Move OFolderDst。我找不到為什么會出錯。
提前致謝!
代碼:
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
uj5u.com熱心網友回復:
每次Move呼叫時,集合都會減少一項。因此,我建議使用反向for回圈來從檔案夾中移動專案。例如:
for i = Items.Count to 1 step -1
Items(i).Move folder
next
因此,在代碼中,您可能會獲取所有未讀專案(由Items集合表示),然后呼叫反向回圈來移動專案。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qianduan/443984.html
下一篇:從客戶端發送檔案到服務器出錯
