Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

For i = 1 To myInbox.Items.Count Problem 1

Status
Not open for further replies.

FateFirst

Programmer
Apr 15, 2002
212
GB
Hi there!

I am having trouble with some code. I recently posted about a solution to saving out emails as text files, and was given a solution (thanks NipsMG!). I have since modified the code to then move the mail items to another folder once they had been saved out.

The whole process pretty much works ok, but I have a slight problem with the 'For i = 1 To myInbox.Items.Count' bit.

For me it seems to get stuck in a loop when i has a value of 10, so it then saves out the same email 10 times and doesnt actually move on to the next email.

Here is the code:
-------------------------------------------
Sub ExportMail()

Dim sFileName As String

On Local Error Resume Next
Dim oNS As NameSpace
Set oNS = ThisOutlookSession.GetNamespace("MAPI")

Dim MyMailbox As MAPIFolder
Dim myInbox As MAPIFolder
Dim i As Integer

Set MyMailbox = oNS.Folders.Item("Personal Folders") 'Very root name of your outlook folder dirs
Set myInbox = MyMailbox.Folders.Item("Bouncebacks") 'Where the mail is located

For i = 1 To myInbox.Items.Count
sFileName = "c:\Temp\MyMail\"
Set oItem = myInbox.Items.Item(i)
sFileName = sFileName & i & ".txt"
oItem.SaveAs sFileName, olTXT
DoEvents
'This folder needs to be within the folder where the mail is located
Set myDestFolder = myInbox.Folders("Done")
oItem.Move myDestFolder
sFileName = ""
DoEvents
Next i

End Sub
-------------------------------------------

I hope someone can help, or give me an idea of where I am going wrong.

Many thanks! - FateFirst
 
My apologies...missed out info.

I have been testing this code with 20 sample emails in the folder. So when 'i' has the value of 10, it just saves out the current mail item 10 more times.

So at the end I have 10 emails still in the first directory, 20 emails in the 'moveto' dir, and 10 of them are duplicates.

The 20 text files it creates also contain 10 duplicates.

Hope this little extra bit of text helps.

:) - FateFirst
 
Hey Fate..

I had the SAME PROBLEM.
Reason Why:


Let's say you have a folder..
Code:
Items    Index
------  --------
1          1
2          2
3          3
4          4

and you run a loop i = 1 to 4.

When i = 1, you move item at index 1
Code:
Items    Index
------  --------
2          1
3          2
4          3
Now i = 2

you move item at index 2
Code:
Items    Index
------  --------
2          1
4          2

See what happens? It's ugly

What I ended up doing was this:

Code:
Do Until MyBox.Items.Count = 0   
        MyBox.Items.Item(1).Move MyPersonalFolder
Loop


--NipsMG
s-)
 
Cheers again, but your not going to believe this but I just worked that out....im such a lamer!

Sorry about that!

Thanks again...you definitely get my vote...again :) - FateFirst
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top