473,320 Members | 2,080 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

Problem with for each

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

Nov 21 '05 #1
1 1113
Comments inline
"Jarod" <Ja***@discussions.microsoft.com> wrote in message
news:1B**********************************@microsof t.com...
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
Nov 21 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: littlefitzer | last post by:
Hi, I have come across a tricky little problem, I hope maybe one of you can help. The problem I am having is that I need to parse two seperate values from an XML document using XSL. The two...
5
by: Nick Shaw | last post by:
Hi, I'm trying to put my resume on my website, and I want to do it using XML and XSL. If I don't include the schema in the mix, the xml displays without a problem. When I add the schema...
16
by: cody | last post by:
I have to write an algorithm with must ensure that objects are put in buckets (which are always 4 in size). The objects have two properties: A and B. It is not allowed that in a bucket are objects...
4
by: Leslaw Bieniasz | last post by:
Cracow, 20.09.2004 Hello, I need to implement a library containing a hierarchy of classes together with some binary operations on objects. To fix attention, let me assume that it is a...
9
by: tym | last post by:
HELP!!! I'm going round the twist with this... I have a VB6 application which is using DAO to access a database (Please - no lectures on ADO, I know what I'm doing with DAO!!) Ok, problem...
0
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
5
by: B. Chernick | last post by:
Is this a bug or just bad programming? I've never encountered this problem before. (Bare minimum sample form to illustrate.) I've also tried this with a class. Same result. The error is: For...
8
by: Elliot Temple | last post by:
Problem: Randomly generate 10 integers from 0-100 inclusive, and sum them. Do that twice. What is the probability the two sums are 390 apart? I have code to do part of it (below), and I know how...
4
by: Christofer Dutz | last post by:
Hi, I am having a small problem, that is driving me nuts. My application reads some Xml and runs 2 Xsl Transformations to generate HTML. As soon as my second XSL introduces some <br/tags, the...
9
by: HC | last post by:
Hello, all, I started out thinking my problems were elsewhere but as I have worked through this I have isolated my problem, currently, as a difference between MSDE and SQL Express 2005 (I'll just...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.