Access Program Only Looping Part Way Through Outlook Inbox

  • Thread starter Thread starter Rich Locus
  • Start date Start date
Status
Not open for further replies.
R

Rich Locus

Hello Outlook Group:

I am developing an Access application that reads through the Inbox (both

read and unread mail), and under certain conditions, adds the mail

information to an Access database. When I am done looking at the mail item,

I file it one one of two folders: either REJECTS or SAVED MAIL. I'm using

POP3.

The problem is that it only loops about half-way through the inbox and exits

BEFORE all the mail in the inbox folder is processed. I have two sub-folders

under the inbox, one call REJECTS and the other SAVED MAIL.

If I do a ? InboxItems.COUNT, the count of the mail is correct... i.e. it

will say I have 7 emails, and that's the correct number, but it only loops

through about 4 times instead of 7 and leaves mail in the INBOX. The code

follows. Any ideas?

Option Compare Database

Option Explicit

Public Function ReadInboxAndMoveV1()

Dim TempRst As DAO.Recordset

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 db As DAO.Database

DoCmd.SetWarnings False

DoCmd.RunSQL "Delete * from tbl_outlooktemp"

DoCmd.SetWarnings True

Set db = CurrentDb

Set OlApp = CreateObject("Outlook.Application")

Set Inbox = OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox)

Set SavedMailFolder =

OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox).Folders("Saved

Mail")

Set RejectMailFolder =

OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox).Folders("Rejects")

Set TempRst = CurrentDb.OpenRecordset("tbl_OutlookTemp")

'

Set InboxItems = Inbox.Items

'

For Each Mailobject In InboxItems

If UCase(Left(Mailobject.Subject, 6)) <> "CLIENT" Then

Mailobject.UnRead = False

Set SavedMailItems = Mailobject.Move(RejectMailFolder)

Else

With TempRst

> AddNew

!Subject = Mailobject.Subject

!from = Mailobject.SenderName

!To = Mailobject.To

!Body = Mailobject.Body

!DateSent = Mailobject.SentOn

> Update

Mailobject.UnRead = False

Set SavedMailItems = Mailobject.Move(SavedMailFolder)

End With

End If

Next

Set TempRst = Nothing

Set OlApp = Nothing

Set Inbox = Nothing

Set SavedMailFolder = Nothing

Set InboxItems = Nothing

Set SavedMailItems = Nothing

Set Mailobject = Nothing

End Function

Any help would be appreciated!!!

Rich Locus

Logicwurks, LLC
 
One more fact. If I keep running the Access macro that runs the code listed

above, eventually all the mail gets processed. It's just a mystery why it

quits early. For example, if I have 10 emails in my Inbox, it might clear

out 6 and leave 4 in the INBOX. The next time I run it, it might process 3

of the 4 and leave 1. Then the third time I run it, it will finally clear

out the email. VERY STRANGE!!!

Rich Locus

Logicwurks, LLC

"Rich Locus" wrote:


> Hello Outlook Group:

> I am developing an Access application that reads through the Inbox (both
> read and unread mail), and under certain conditions, adds the mail
> information to an Access database. When I am done looking at the mail item,
> I file it one one of two folders: either REJECTS or SAVED MAIL. I'm using
> POP3.

> The problem is that it only loops about half-way through the inbox and exits
> BEFORE all the mail in the inbox folder is processed. I have two sub-folders
> under the inbox, one call REJECTS and the other SAVED MAIL.

> If I do a ? InboxItems.COUNT, the count of the mail is correct... i.e. it
> will say I have 7 emails, and that's the correct number, but it only loops
> through about 4 times instead of 7 and leaves mail in the INBOX. The code
> follows. Any ideas?

> Option Compare Database
> Option Explicit

> Public Function ReadInboxAndMoveV1()
> Dim TempRst As DAO.Recordset
> 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 db As DAO.Database

> DoCmd.SetWarnings False
> DoCmd.RunSQL "Delete * from tbl_outlooktemp"
> DoCmd.SetWarnings True
> Set db = CurrentDb

> Set OlApp = CreateObject("Outlook.Application")
> Set Inbox = OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox)
> Set SavedMailFolder =
> OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox).Folders("Saved
> Mail")
> Set RejectMailFolder =
> OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox).Folders("Rejects")
> Set TempRst = CurrentDb.OpenRecordset("tbl_OutlookTemp")
> '
> Set InboxItems = Inbox.Items
> '
> For Each Mailobject In InboxItems
> If UCase(Left(Mailobject.Subject, 6)) <> "CLIENT" Then
> Mailobject.UnRead = False
> Set SavedMailItems = Mailobject.Move(RejectMailFolder)
> Else
> With TempRst
> .AddNew
> !Subject = Mailobject.Subject
> !from = Mailobject.SenderName
> !To = Mailobject.To
> !Body = Mailobject.Body
> !DateSent = Mailobject.SentOn
> .Update
> Mailobject.UnRead = False
> Set SavedMailItems = Mailobject.Move(SavedMailFolder)
> End With
> End If
> Next

> Set TempRst = Nothing
> Set OlApp = Nothing
> Set Inbox = Nothing
> Set SavedMailFolder = Nothing
> Set InboxItems = Nothing
> Set SavedMailItems = Nothing
> Set Mailobject = Nothing

> End Function

> Any help would be appreciated!!!
> > Rich Locus
> Logicwurks, LLC
 
Moderator:

Could you please delete this post? I'm going to put in a simpler example.

Rich Locus

Logicwurks, LLC

"Rich Locus" wrote:


> Hello Outlook Group:

> I am developing an Access application that reads through the Inbox (both
> read and unread mail), and under certain conditions, adds the mail
> information to an Access database. When I am done looking at the mail item,
> I file it one one of two folders: either REJECTS or SAVED MAIL. I'm using
> POP3.

> The problem is that it only loops about half-way through the inbox and exits
> BEFORE all the mail in the inbox folder is processed. I have two sub-folders
> under the inbox, one call REJECTS and the other SAVED MAIL.

> If I do a ? InboxItems.COUNT, the count of the mail is correct... i.e. it
> will say I have 7 emails, and that's the correct number, but it only loops
> through about 4 times instead of 7 and leaves mail in the INBOX. The code
> follows. Any ideas?

> Option Compare Database
> Option Explicit

> Public Function ReadInboxAndMoveV1()
> Dim TempRst As DAO.Recordset
> 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 db As DAO.Database

> DoCmd.SetWarnings False
> DoCmd.RunSQL "Delete * from tbl_outlooktemp"
> DoCmd.SetWarnings True
> Set db = CurrentDb

> Set OlApp = CreateObject("Outlook.Application")
> Set Inbox = OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox)
> Set SavedMailFolder =
> OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox).Folders("Saved
> Mail")
> Set RejectMailFolder =
> OlApp.GetNamespace("Mapi").GetDefaultFolder(olFolderInbox).Folders("Rejects")
> Set TempRst = CurrentDb.OpenRecordset("tbl_OutlookTemp")
> '
> Set InboxItems = Inbox.Items
> '
> For Each Mailobject In InboxItems
> If UCase(Left(Mailobject.Subject, 6)) <> "CLIENT" Then
> Mailobject.UnRead = False
> Set SavedMailItems = Mailobject.Move(RejectMailFolder)
> Else
> With TempRst
> .AddNew
> !Subject = Mailobject.Subject
> !from = Mailobject.SenderName
> !To = Mailobject.To
> !Body = Mailobject.Body
> !DateSent = Mailobject.SentOn
> .Update
> Mailobject.UnRead = False
> Set SavedMailItems = Mailobject.Move(SavedMailFolder)
> End With
> End If
> Next

> Set TempRst = Nothing
> Set OlApp = Nothing
> Set Inbox = Nothing
> Set SavedMailFolder = Nothing
> Set InboxItems = Nothing
> Set SavedMailItems = Nothing
> Set Mailobject = Nothing

> End Function

> Any help would be appreciated!!!
> > Rich Locus
> Logicwurks, LLC
 
Status
Not open for further replies.
Similar threads
Thread starter Title Forum Replies Date
T Outlook "A program is trying to access Outlook" Using Outlook 3
D Outlook 2003 - A program is trying to access e-mail addresses Outlook VBA and Custom Forms 5
V how to switchoff program access message in outlook 2003 Outlook VBA and Custom Forms 1
E "Cannot display the folder. MS Outlook cannot access the specified file location" Using Outlook 5
J Outlook, Word, Access crash unless run in Admin mode - time dependent it seems Using Outlook 10
Victor_50 Problem - Google Workspace will stop "unsafe" access to Outlook end 2024 Using Outlook 3
A Quick Access Toolbar Not Showing Description Using Outlook 0
W Outlook 365 File access denied attempting to import .pst Using Outlook 6
J Recover server side rules from OST/PST without access to the server Using Outlook 2
richardwing Outlook 365 VBA to access "Other Actions" menu for incoming emails in outlook Outlook VBA and Custom Forms 0
V How to add 'Previous Item' and 'Next Item' to the Quick Access Toolbar Using Outlook 1
J Text icon in Quick Access toolbar ? Using Outlook 2
Z Import Tasks from Access Using VBA including User Defined Fields Outlook VBA and Custom Forms 0
klpconsulting Programmatic Access Office 365 Pro Plus & RDS Using Outlook 1
Fozzie Bear Shared Public Folders Access and Use Exchange Server Administration 0
G How to add a folder shortcut to outlook quick access toolbar? Using Outlook 6
D Lost Access to Custom Form Outlook VBA and Custom Forms 4
Jennifer Murphy Grant R/W (update) access to a friend Using Outlook 3
O The page that you are trying to access cannot be loaded. Using Outlook 0
T Render Outlook emails in MS access as they appear in Outlook Outlook VBA and Custom Forms 2
GregS Can't access archive file Using Outlook 5
J What is the best EntryID format to set on MS Access table Outlook VBA and Custom Forms 3
J No access to a Database BCM BCM (Business Contact Manager) 3
R How can I access my emails from corrupt offline files of MS Outlook ? Using Outlook 1
V Change default default save location to Quick Access Using Outlook 1
R Quick Access view in File Explorer when saving attachments Using Outlook 0
D Disable or hide "reply" and "reply to all" and "forward" in email from access vba Outlook VBA and Custom Forms 1
J Open an outlook email by Subject on MS Access linked table with VBA Outlook VBA and Custom Forms 10
G Windows Update Causes BCM Database Access Problem? BCM (Business Contact Manager) 4
D populating listbox on custom form from Access Outlook VBA and Custom Forms 7
O Synchronize safe persons Outlook 2016 -> Outlook Web Access (OWA) Using Outlook 30
K How to access emails found to be located in "Top of Outlook data file"? Using Outlook 3
R Categorize Button in Quick Access Toolbar Disappears on New Email Using Outlook 1
O Outlook Web Access - how to disable spam filter Using Outlook 6
T Lost access to remote BCM database BCM (Business Contact Manager) 4
F Outlook 2016 bulk email from access Outlook VBA and Custom Forms 3
J Programmatic access grayed out Using Outlook 10
Diane Poremsky Synchronize Quick Access Toolbar and Ribbons? Using Outlook 0
G Can't open .pst. Message could not access default folder (Outlook 2010 (.pst). Before that was backi Using Outlook 0
D Creating an outlook session from Access vba but run silently. With A specific profile Outlook VBA and Custom Forms 1
T Cannot access outlook.com accounts in Outlook 2016 Using Outlook 1
B Can't expose sender email address when linking outlook to access Using Outlook 3
Diane Poremsky Change Outlook's Programmatic Access Options Using Outlook 0
S how to access the properties of a contact given distlist.member object Outlook VBA and Custom Forms 1
C OWA External Access - No URL / URL Invalid Exchange Server Administration 2
G Adding a contact to Outlook with a custom form using Access VBA Outlook VBA and Custom Forms 1
C Merging Word and Access into Outlook Using Outlook 4
I Outlook 2016 and Slow access to Outlook.com IMAP Using Outlook 0
B No permission to access Outlook.pst file Using Outlook 1
Norbert VBA Flag that would indicate the email was sent from Outlook to Access. Outlook VBA and Custom Forms 2

Similar threads

Back
Top