Move message skips messages when looping through mailbox

  • Thread starter Thread starter John_Ba
  • Start date Start date
Status
Not open for further replies.
J

John_Ba

I'm trying to loop through a mailbox and display a form that chooses whether

to move the message to a different folder and which one to move it to.

For testing I have 2 messages in the mailbox. The problem is that if I choose

to move the firts message then the loop ignores the second message, however

if I don't move the first message then the second message is displayed.

If I run in debug mode with a breakpoint then the code works as expected.

Anybody have a good idea?

Set myOlApp = CreateObject("Outlook.Application")

Set myNameSpace = myOlApp.GetNamespace("MAPI")

Set myMailbox = myNameSpace.folders("Mailbox - RSP SWE DMSII Support")

Set myInbox = myMailbox.folders("Inbox")

'

Set AgentRS = OpenRS(myConn, "SELECT * FROM AgentsColleagues('" & USERCK

& "') ORDER BY Name")

Set AgentForm = New GetAgent

AgentForm.SetAgentList = AgentRS

AgentForm.SetFolderList = myMailbox

'

For Each myItem In myInbox.Items

myItem.Display

' Debug.Print myItem.UserProperties("Owned by")

AgentForm.Init = -1

AgentForm.Show

If AgentForm.AgentList.ListIndex > 0 Then

Set Owner = myItem.UserProperties.Add("Owned by", olText, True)

Owner.Value = AgentForm.AgentList

End If

Set Action = myItem.UserProperties.Add("Action", olText, True)

Action.Value = "NEW"

If AgentForm.FolderList.ListIndex > 0 Then

destFolder = AgentForm.FolderList.List(AgentForm.FolderList.

ListIndex)

myItem.Move (myMailbox.folders(destFolder))

Else

myItem.Close olSave

End If

DoEvents

Next myItem

CloseRS AgentRS

Unload AgentForm

Set AgentForm = Nothing
 
When you are removing items from a collection never use For Each or count up

For loops since you are relying on an index that you are altering as you

remove the items. Use a count down For loop or a Do loop instead:

For i = myInbox.Items.Count To 1 Step -1

myItem = myInbox.Items(i)

' etc.

"John_Ba" <u54813@uwe> wrote in message news:9c31bd035d3e7@uwe...
> I'm trying to loop through a mailbox and display a form that chooses
> whether
> to move the message to a different folder and which one to move it to.
> For testing I have 2 messages in the mailbox. The problem is that if I
> choose
> to move the firts message then the loop ignores the second message,
> however
> if I don't move the first message then the second message is displayed.
> If I run in debug mode with a breakpoint then the code works as expected.
> Anybody have a good idea?

> Set myOlApp = CreateObject("Outlook.Application")
> Set myNameSpace = myOlApp.GetNamespace("MAPI")
> Set myMailbox = myNameSpace.folders("Mailbox - RSP SWE DMSII Support")
> Set myInbox = myMailbox.folders("Inbox")
> '
> Set AgentRS = OpenRS(myConn, "SELECT * FROM AgentsColleagues('" &
> USERCK
> & "') ORDER BY Name")
> Set AgentForm = New GetAgent
> AgentForm.SetAgentList = AgentRS
> AgentForm.SetFolderList = myMailbox
> '
> For Each myItem In myInbox.Items
> myItem.Display
> ' Debug.Print myItem.UserProperties("Owned by")
> AgentForm.Init = -1
> AgentForm.Show

> If AgentForm.AgentList.ListIndex > 0 Then
> Set Owner = myItem.UserProperties.Add("Owned by", olText, True)
> Owner.Value = AgentForm.AgentList
> End If

> Set Action = myItem.UserProperties.Add("Action", olText, True)
> Action.Value = "NEW"

> If AgentForm.FolderList.ListIndex > 0 Then
> destFolder = AgentForm.FolderList.List(AgentForm.FolderList.
> ListIndex)
> myItem.Move (myMailbox.folders(destFolder))
> Else
> myItem.Close olSave
> End If
> DoEvents
> Next myItem
> CloseRS AgentRS

> Unload AgentForm
> Set AgentForm = Nothing
>
 
Ken,

Thanks very much, that was very helpfull. Pity it doesn't occur in more

programming examples of looping through items. I searched the web and MSDN

looking for a way to do this.
- wrote:
> When you are removing items from a collection never use For Each or count up
> For loops since you are relying on an index that you are altering as you
> remove the items. Use a count down For loop or a Do loop instead:

> For i = myInbox.Items.Count To 1 Step -1
> myItem = myInbox.Items(i)
> ' etc.
>
> > I'm trying to loop through a mailbox and display a form that chooses
> > whether

> [quoted text clipped - 46 lines]
> > Unload AgentForm
> > Set AgentForm = Nothing


 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
HarvMan Outlook 365 - Rule to Move an Incoming Message to Another Folder Using Outlook 4
Commodore Unable to move message Using Outlook 3
T Macro to move reply and original message to folder Outlook VBA and Custom Forms 6
I Automating message move between folders Outlook VBA and Custom Forms 0
E Search for folder by key in subject then move new message to related folder Outlook VBA and Custom Forms 1
mrmmickle1 UserProperties VBA to move message to another folder Outlook VBA and Custom Forms 7
T What code to use to move the custom field values to message body when sending e-mail ? Outlook VBA and Custom Forms 8
M Automatically move Outlook message to the specific folder when click on reply to all Using Outlook 4
M Error message: "Cannot move the items..." Using Outlook 2
M Outlook 2010 IMAP account message move behavior Using Outlook 15
mikolajek Outlook 2013 IMAP actions (message move / delete etc.) significantly delayed Using Outlook 0
D Error message - Cannot move the items.... Using Outlook 1
H move to folder rule corrupts message Using Outlook 3
A How do I move a message to a folder after forwarding ? Outlook VBA and Custom Forms 6
A How to catch Message Move Event. Outlook VBA and Custom Forms 10
H Move Selected emails to Local Drive Outlook VBA and Custom Forms 0
A Search folder and move the email Outlook VBA and Custom Forms 0
A Outlook 365 (OutLook For Mac)Move "On My Computer" Folder Items From Old To New Mac Computer Using Outlook 4
humility36 Cannot move emails to archive - 440 error Outlook VBA and Custom Forms 1
B Outlook 2019 Automatically move email after assigning category Using Outlook 4
C Trying to move messages between imap accounts/folders Using Outlook 5
M Move command Outlook VBA and Custom Forms 11
C Code to move mail with certain attachment name? Does Not work Outlook VBA and Custom Forms 3
B Move emails from one account to another Outlook VBA and Custom Forms 2
J Quick steps delete original email and move reply/sent email to folder Using Outlook 2
N How to add or delete items to Move dropdown Menu Using Outlook 0
N Line to move origEmail to subfolder within a reply macro Outlook VBA and Custom Forms 0
C Move or copy from field to field Outlook VBA and Custom Forms 0
T Outlook 365 Move newly created tasks automatically on save. Outlook VBA and Custom Forms 1
NVDon Create new Move To Folder list Outlook VBA and Custom Forms 0
P Print attachments automatically and move the mail to an existing folder called "Ted" Outlook VBA and Custom Forms 4
F VBA to move email from Non Default folder to Sub folders as per details given in excel file Outlook VBA and Custom Forms 11
F Excel VBA to move mails for outlook 365 on secondary mail account Outlook VBA and Custom Forms 1
J Dopey move - deleted profile Using Outlook 1
GregS Outlook 2016 Move Outlook to new computer? Using Outlook 4
Witzker Macro to move @domain.xx of a Spammail to Blacklist in Outlook 2019 Outlook VBA and Custom Forms 7
G Move tasks up/down todo list by VBA Outlook VBA and Custom Forms 1
S Macro to move “Re:” & “FWD:” email recieved the shared inbox to a subfolder in outlook Outlook VBA and Custom Forms 0
S Outlook Macro to move reply mail based on the key word in the subjectline Outlook VBA and Custom Forms 0
D Move Email with Attachment to Folder Outlook VBA and Custom Forms 3
Eike Move mails via macro triggered by the click of a button? Outlook VBA and Custom Forms 0
G Cannot Move Autocomplete File to New Computer Using Outlook 15
M Move to Folder Using Outlook 1
P Move emails between 2 mailboxes. Using Outlook 0
C Copy Move item won't work Outlook VBA and Custom Forms 2
N Macro to move all recipients to CC while replying Outlook VBA and Custom Forms 0
Commodore Move turns into "copy" Using Outlook 3
R List folders in a combo box + select folder + move emails from inbox to that folder + reply to that email Outlook VBA and Custom Forms 1
Jennifer Murphy Ctrl+Tab sometimes will not move through text a word at a time Using Outlook 1
V Outlook 2016 will not move emails in search results Using Outlook 4

Similar threads

Back
Top