R
Rich Locus
Outlook Group:
Here is a simple program run from Microsoft Access that loops through the
Outlook Inbox and moves the mail from the Inbox to one of two folders: "Saved
Mail" or "Rejects". These are user folders at the same level as all the
other standard folders.. (i.e. they are not underneath any of the standard
folders).
Each time it is run, it only moves one half of the items in the inbox.
So, if there are 20 mails in the Inbox, and I run the program, it only moves
10 items to the user folders and leaves 10 still in the Inbox.
If I run it a second time, then the inbox goes from 10 to 5.
When I run it a third time, the inbox goes to 3.
Next time, to 1.
And finally all the mail is moved.
It divides it in half each time.
Unless I'm missing something, this bug is related to the fact that I'm
moving items to a different folder. The bug DOES NOT OCCUR if I don't move
the mail to another folder.
Here's the code. Any comments would be appreciated.
Option Compare Database
Option Explicit
Public Function IllustrateLoopBug()
Dim OlApp As Outlook.Application
Dim Inbox As Outlook.MAPIFolder
Dim SavedMailFolder As Outlook.MAPIFolder
Dim RejectMailFolder As Outlook.MAPIFolder
Dim InboxItems As Outlook.Items
Dim SavedMailItems As Outlook.MailItem
Dim RejectMailItems As Outlook.MailItem
Dim Mailobject As Object
Dim intMailItems As Integer
Dim intCountedLoops As Integer
intCountedLoops = 0
Set OlApp = CreateObject("Outlook.Application")
Set Inbox = OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox)
Set SavedMailFolder =
OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox).Parent.Folders("Saved Mail")
Set RejectMailFolder =
OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox).Parent.Folders("Rejects")
Set InboxItems = Inbox.Items
intMailItems = InboxItems.Count
intCountedLoops = 0
For Each Mailobject In InboxItems
intCountedLoops = intCountedLoops + 1
If UCase(Left(Mailobject.Subject, 6)) <> "CLIENT" Then
Mailobject.UnRead = False
Set SavedMailItems = Mailobject.Move(RejectMailFolder)
Else
Mailobject.UnRead = False
Set SavedMailItems = Mailobject.Move(SavedMailFolder)
End If
Next
If intMailItems <> intCountedLoops Then
MsgBox ("Mail Items = " & intMailItems & ", Counted Loops = " &
intCountedLoops)
End If
Set OlApp = Nothing
Set Inbox = Nothing
Set SavedMailFolder = Nothing
Set RejectMailFolder = Nothing
Set InboxItems = Nothing
Set SavedMailItems = Nothing
Set RejectMailItems = Nothing
Set Mailobject = Nothing
End Function
Here is a simple program run from Microsoft Access that loops through the
Outlook Inbox and moves the mail from the Inbox to one of two folders: "Saved
Mail" or "Rejects". These are user folders at the same level as all the
other standard folders.. (i.e. they are not underneath any of the standard
folders).
Each time it is run, it only moves one half of the items in the inbox.
So, if there are 20 mails in the Inbox, and I run the program, it only moves
10 items to the user folders and leaves 10 still in the Inbox.
If I run it a second time, then the inbox goes from 10 to 5.
When I run it a third time, the inbox goes to 3.
Next time, to 1.
And finally all the mail is moved.
It divides it in half each time.
Unless I'm missing something, this bug is related to the fact that I'm
moving items to a different folder. The bug DOES NOT OCCUR if I don't move
the mail to another folder.
Here's the code. Any comments would be appreciated.
Option Compare Database
Option Explicit
Public Function IllustrateLoopBug()
Dim OlApp As Outlook.Application
Dim Inbox As Outlook.MAPIFolder
Dim SavedMailFolder As Outlook.MAPIFolder
Dim RejectMailFolder As Outlook.MAPIFolder
Dim InboxItems As Outlook.Items
Dim SavedMailItems As Outlook.MailItem
Dim RejectMailItems As Outlook.MailItem
Dim Mailobject As Object
Dim intMailItems As Integer
Dim intCountedLoops As Integer
intCountedLoops = 0
Set OlApp = CreateObject("Outlook.Application")
Set Inbox = OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox)
Set SavedMailFolder =
OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox).Parent.Folders("Saved Mail")
Set RejectMailFolder =
OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox).Parent.Folders("Rejects")
Set InboxItems = Inbox.Items
intMailItems = InboxItems.Count
intCountedLoops = 0
For Each Mailobject In InboxItems
intCountedLoops = intCountedLoops + 1
If UCase(Left(Mailobject.Subject, 6)) <> "CLIENT" Then
Mailobject.UnRead = False
Set SavedMailItems = Mailobject.Move(RejectMailFolder)
Else
Mailobject.UnRead = False
Set SavedMailItems = Mailobject.Move(SavedMailFolder)
End If
Next
If intMailItems <> intCountedLoops Then
MsgBox ("Mail Items = " & intMailItems & ", Counted Loops = " &
intCountedLoops)
End If
Set OlApp = Nothing
Set Inbox = Nothing
Set SavedMailFolder = Nothing
Set RejectMailFolder = Nothing
Set InboxItems = Nothing
Set SavedMailItems = Nothing
Set RejectMailItems = Nothing
Set Mailobject = Nothing
End Function