Problem with for each

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

Hey
I wrote program to collect my attachments in one folder. I am using OE 2003.
The problem is , when I am using this program it doesn't took every mail it
leaves some of them. When I run it for second time it usually takes care
about the rest, but it should do it in one run. I am using following code:

For Each itm As Outlook.MailItem In fld.Items

For Each atch As Outlook.Attachment In itm.Attachments
Dim fi As New System.IO.FileInfo(atch.FileName)
Dim fname As String

Dim AttFile As System.IO.File
fname = pathNew & atch.FileName
If AttFile.Exists(fname) Then

' It gives just a random name for a file if the file with such a name
already exists

While AttFile.Exists(fname)
Dim rnd As New Random
fname = pathNew & rnd.Next(100, 999) &
fi.Extension
End While
End If

Try
atch.SaveAsFile(fname)
Catch ex As IO.IOException
MsgBox(ex.Message)
End Try
hasAttachment = True
Next

If hasAttachment Then
itm.Delete()
hasAttachment = False
End If
Next

I put the code above in a for loop to run it 5 times and it works perfectly,
why it doesn't clean everything in a first run ?
Jarod
 
Comments inline
Jarod said:
Hey
I wrote program to collect my attachments in one folder. I am using OE
2003.
The problem is , when I am using this program it doesn't took every mail
it
leaves some of them. When I run it for second time it usually takes care
about the rest, but it should do it in one run. I am using following code:

For Each itm As Outlook.MailItem In fld.Items

For Each atch As Outlook.Attachment In itm.Attachments
Dim fi As New System.IO.FileInfo(atch.FileName)
Dim fname As String

Dim AttFile As System.IO.File
fname = pathNew & atch.FileName
If AttFile.Exists(fname) Then

' It gives just a random name for a file if the file with such a name
already exists

While AttFile.Exists(fname)
Dim rnd As New Random
fname = pathNew & rnd.Next(100, 999) &
fi.Extension
End While
End If

Try
atch.SaveAsFile(fname)
Catch ex As IO.IOException
MsgBox(ex.Message)
End Try
hasAttachment = True
Next
If hasAttachment Then
itm.Delete() <--------PROBLEM
hasAttachment = False
End If
Next
The problem is you are altering the collection while you are enumerating
through it which is generally considered a no-no. Some collections will
throw exceptions in this case; others handle it in more subtle ways. In
this case the Delete call is immediately moving the current pointer to the
next item in the list; then when the code gets to the Next line it moves it
again, effectively skipping items.

The best way to handle this is to add the items that you want to delete to a
separate collection and then delete them outside the main For Each/Next
loop.
I put the code above in a for loop to run it 5 times and it works
perfectly,
why it doesn't clean everything in a first run ?
Jarod

Hope this helps,

Nick Hall
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top