刪除 [刪除的專案] 資料夾中的所有專案和子資料夾

這個主題會說明一個 Visual Basic for Applications (VBA) 程式碼範例,範例中會刪除 [刪除的郵件] 資料夾中的所有郵件及子資料夾。

注意 當您從集合中刪除專案或資料夾時,必須使用遞減迴圈計數器。 遞增迴圈計數器將會失敗。

[刪除的郵件] 資料夾只能清空,不能移除資料夾本身。 不過,若要刪除 [刪除的郵件] 資料夾的子資料夾,可以直接刪除子資料夾,不必先刪除其內容。

Sub RemoveAllItemsAndFoldersInDeletedItems() 
 Dim oDeletedItems As Outlook.Folder 
 Dim oFolders As Outlook.Folders 
 Dim oItems As Outlook.Items 
 Dim i As Long 
 'Obtain a reference to deleted items folder 
 Set oDeletedItems = Application.Session.GetDefaultFolder(olFolderDeletedItems) 
 Set oItems = oDeletedItems.Items 
 For i = oItems.Count To 1 Step -1 
 oItems.Item(i).Delete 
 Next 
 Set oFolders = oDeletedItems.Folders 
 For i = oFolders.Count To 1 Step -1 
 oFolders.Item(i).Delete 
 Next 
End Sub

支援和意見反應

有關於 Office VBA 或這份文件的問題或意見反應嗎? 如需取得支援服務並提供意見反應的相關指導,請參閱 Office VBA 支援與意見反應