neilmaclean
Programmer
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 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 ?????