Reputation: 37
I am trying to move messages that are marked as Read from my errorMails
folder to my sentErrors
folder, both of which are within my mailOne
folder. The current code I have works for most of the mail I have marked as read, but leaves one last read mail alone.
Am I missing something?
Public Sub moveToSentFolder()
Dim obj As Object
Dim Items As Outlook.Items
Dim OutMail As Outlook.MailItem
Dim archiveFolder As Outlook.Folder
Dim mailOneFolder As Outlook.Folder
Dim sentErrorFolder As Outlook.Folder
Dim dumpErrorFolder As Outlook.Folder
Set archiveFolder = Outlook.Session.Folders("Archives")
Set mailOneFolder = archiveFolder.Folders("mailOne")
Set errorFolder = ehealthFolder.Folders("errorMails")
Set dumpErrorFolder = ehealthFolder.Folders("sentErrors")
'Dim Message As MailItem
Set Folder = Application.Session.GetDefaultFolder(olFolderInbox)
Set Items = errorFolder.Items
For i = Items.Count - 1 To 0 Step -1
With Items(i)
''Error 438 is returned when .receivedtime is not supported
On Error Resume Next
If .UnRead = False Then
If Err.Number = 0 Then
.Move dumpErrorFolder
Else
Err.Clear
End If
End If
MsgBox i 'debug
End With
Next
'For some reason the commented out code below only seems to move half of all the read mails, so I have to run the macro more than once to clear folder of read mails - this code now unused
'For Each Message In Items
'If Message.UnRead = False Then
''Message.Move dumpErrorFolder
'i = i + 1
'End If
'Next Message
End Sub
Upvotes: 1
Views: 1001
Reputation: 31651
In VBA, items can have varying index boundaries - controlled using option base
or To
clause. In Office, arrays are indexed starting at position 1 (1-based), not 0 (0-based). You need to change your FOR
loop to accommodate this change.
For i = Items.Count To 1 Step -1
As another alternative, you could leverage UBound
and LBound
to determine the appropriate index boundaries.
For i = UBound(Items) To LBound(Items) Step -1
Upvotes: 2