Moving Mail

N

Nathan Carroll

why in procedure below do not all of the email messages get moved form the
loop process

Private Sub Application_Startup()
Dim item As Object
Dim subfolder As MAPIFolder
Dim mi As MailItem

On Error Resume Next
CreateButton

Set inBoxItems =
Me.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items
For Each item In inBoxItems
If item.Class = olMail Then
Set mi = item
Set subfolder =
Me.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox) _
.Folders(mi.SenderName)
If subfolder Is Nothing Then
Me.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox) _
.Folders.Add (mi.SenderName)
Set subfolder =
Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox) _
.Folders(mi.SenderName)
End If
mi.Move subfolder
End If
Next

End Sub
 
D

Dmitry Streblechenko

Because the number of messages changes as you go through the loop.
Use something like
While inBoxItems.Count > 0
set mi = inBoxItems.Item(1)
...

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
N

Nathan Carroll

thanks
Dmitry Streblechenko said:
Because the number of messages changes as you go through the loop.
Use something like
While inBoxItems.Count > 0
set mi = inBoxItems.Item(1)
...

Dmitry Streblechenko (MVP)
http://www.dimastr.com/
OutlookSpy - Outlook, CDO
and MAPI Developer Tool
 
N

Nathan Carroll

Ended up using a collection like so:
Seemed a bit faster.

Dim mailitems As Collection

Private Sub Application_Startup()
Dim item As Object
Dim subfolder As MAPIFolder
Dim mi As MailItem

'On Error Resume Next
CreateButton
Set mailitems = New Collection
Set inBoxItems =
Me.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox).Items

For Each item In inBoxItems
If item.Class = olMail Then
Set mi = item
Set mIDs = New clsMailIds
mailitems.Add (mi.EntryID)
End If
Next


Dim o As Variant
Dim s As String

For Each o In mailitems
s = CStr(o)
Set mi = Me.GetNamespace("MAPI").GetItemFromID(s)

Set subfolder =
Me.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox) _
.Folders(mi.SenderName)

If subfolder Is Nothing Then
Me.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox) _
.Folders.Add (mi.SenderName)
Set subfolder =
Application.GetNamespace("MAPI").GetDefaultFolder(olFolderInbox) _
.Folders(mi.SenderName)
End If

mi.Move subfolder

Next

Set subfolder = Nothing
Set mi = Nothing
Set mailitems = Nothing

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top