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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Move email in outlook using visual basic

Status
Not open for further replies.

neilmaclean

Programmer
Jan 27, 2003
4
GB
I have a problem moving emails from one folder to another using the Outlook Object Model from Visual Basic 6.0 (outlook version 2002 - v10.2??)

I have, in my current test, 11 emails in my inbox. I would like to move all of them into a folder called 'test'. For some strange reason not all of them move.

Below is the code that i use.

' Initialise Outlook App
Dim objOutlook As New Outlook.Application

' Initialise name space
Dim objOutlookNameSpace As NameSpace

' Initialise folders
Dim fldInbox As MAPIFolder
Dim fldPersonal As MAPIFolder

' Others
Dim lNumEmailsInInbox As Long
Dim MyMail As Items

' Set the name space to MAPI
Set objOutlookNameSpace = objOutlook.GetNamespace("MAPI")

' Set the pointer to the inbox
Set fldInbox = objOutlookNameSpace.GetDefaultFolder(olFolderInbox)

' Set the pointer to the Test folder
'(a folder within the inbox)
Set fldTest = fldInbox.Folders("Test")

' How many emails in the inbox?
lNumEmailsInInbox = fldInbox.Items.Count

' Display it on a form
txtNumEmails.Text = CStr(lNumEmailsInInbox)

' Initialise Mail items
Dim MyMailItem As Outlook.MailItem
Set MyMail = fldInbox.Items

' If there are any mail items in inbox
If lNumEmailsInInbox > 0 Then

' Loop through each mail in the inbox
' and move it to test folder
For Each MyMailItem In MyMail

Debug.Print ("Moving " + CStr(MyMailItem))
With MyMailItem
.Move fldTest
End With

Next MyMailItem
Else
Call MsgBox("No emails in inbox to move", vbAbortRetryIgnore, "Warning")
End If

......
I have tried to incorporate a massive pause in between moving each mail item but that did not work either.
To be precise, i have 11 emails (subject Test 1 through to Test 11). Sometimes 4 or 5 or 6 will only move. It is not the same ones every time. If i keep running the code all emails will eventually move.

Why does this code not move all emails? Help ?????

 
I've solved this problem. For completeness here is the solution to the problem:

Never use move in a for each loop. The index resets itself after each move, so only half the items will be moved. The solution to this is not to count up through a loop but countdown from the number of emails you want to move, e.g.

For i = lNumEmailsInOriginatingFolder To 1 Step -1

Set MyMailItem = MyOriginatingFolder.Items(i)

Set MyMovedItem = MyMailItem.Move(MyDestination)

Next

 
I have also used something like
do while fldInbox.Items.Count > 0
Set MyMailItem = MyOriginatingFolder.Items(1)

Set MyMovedItem = MyMailItem.Move(MyDestination
Loop
this code runs as long as there is an item in the folder. Just another approach.
sdraper
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top