VB6 Code Exiting For Next Statement Early
Hello, I wonder if anyone can help me with a problem I'm having please.
I have some code which is basically using a timer and checking an Outlook mailbox, and if there is an unread mail in there with an attachement, it strips the attachement out and saves it to a file location.
Code:
For Each item In InBox.Items
vvalid = False
If item.Attachments.Count > 0 And item.UnRead Then
For Each Atmt In item.Attachments
txt1.Text = "PROCESSING FILE..."
Set fso = New FileSystemObject
tempfilename = Atmt.FileName
tempfilename = Replace(tempfilename, " ", "_")
If fso.FolderExists(directory) Then
FileName = directory & tempfilename
Atmt.SaveAsFile FileName
Else
ErrorHandler "Public Sub ErrorHandling"
vvalid = False
End If
Set fso = Nothing
vvalid = True
Next Atmt
If vvalid = True Then
item.UnRead = False
item.Move olMoveFolder
End If
End If
Next item
This all works fine and works very quicly.
However, if there are say 20 e-mails in the inbox the first time the code runs it will process 10 before exiting out of the code rather than processing all 20.
It will then wait a minute until the next timer event ticks over and it will process 4 more.
Initially I thought it was doing this because of the fact that the timer event was ticking over part way through the process, but if that were the case then surely the 2nd time it would also process 10 files?
I need it to process the whole number of files on the first run through, and I can't understand why it's coming out of the code.
Can anyone shed a light on this please?
Re: VB6 Code Exiting For Next Statement Early
if you remove items within your loop the indexes get changed and the next item is skipped
try using a reverse loop
vb Code:
for i = inbox.items.count - 1 to 0 step -1
set item = inbox.items(i)
If item.Attachments.Count > 0 And itemsUnRead Then