1
的,我有以下的代碼來自動刪除舊的兩個多月電子郵件:VBA展望數組索引越界
Sub RemoveAutomaticItemsInDeletedItems()
If MsgBox("Delete items from from Deleted Items folder ?", vbYesNo, "Confirm") = vbYes Then
Dim oDeletedItems As Outlook.Folder
Dim obj As Outlook.MailItem
Dim i As Integer
'Obtain a reference to deleted items folder
Set oDeletedItems = Application.Session.GetDefaultFolder(olFolderDeletedItems)
For i = 1 To oDeletedItems.Items.Count - 1
If oDeletedItems.Items(i).Class = olMail Then
Set obj = oDeletedItems.Items.Item(i)
If obj.ReceivedTime <= DateAdd("m", -2, Now) Then
'Deleting them
Debug.Print obj.SenderEmailAddress
'Debug.Print obj.Subject
obj.Delete
End If
End If
Next
MsgBox "Items have been deleted"
End If
End Sub
但是,錯誤:
Array index out of bounds
不斷叫上線:
If oDeletedItems.Items(i).Class = olMail Then
使用Debug.Print
我已經看過上面的代碼行:oDeletedItems.Items.Count - 1
,計數是1889,因此數組中有元素。
如果數組中有對象,爲什麼這個錯誤會持續存在?
的可能的複製[For Each循環:通過Outlook郵箱中循環,從而刪除項目時,某些項目獲得跳過]( http://stackoverflow.com/questions/10725068/for-each-loop-some-items-get-skipped-when-looping-through-outlook-mailbox-to-de) – niton