VBA not getting all messages

VBA not getting all messages

Post by S2FyZW5 » Thu, 20 Nov 2003 02:51:24


I want to get all unread Outlook messages from an Access 2000 database. Using this code, numMailItems reflects the actual number of unread messages. However, the For...Next loop processes some of the unread messages and then stops, setting objMailItem to nothing. Any ideas

Set golApp = New Outlook.Applicatio
Set objFolder = gnspNameSpace.GetDefaultFolder(olFolderInbox
Set objRetainerItems = objItems.Restrict("[Unread] = True"
numMailItems = objRetainerItems.Coun

For Each objMailItem In objRetainerItem
' processing for each unread messag
Next objMailItem
 
 
 

VBA not getting all messages

Post by Sue Mosher » Thu, 20 Nov 2003 03:03:14

What kind of processing goes on in the loop? If you're moving or deleting items, that's a no-no, because it resets the index. You can use a countdown loop instead.
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.yqcomputer.com/
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
http://www.yqcomputer.com/

 
 
 

VBA not getting all messages

Post by S2FyZW5 » Thu, 20 Nov 2003 03:11:05

I'm parsing the message and inserting data into tables. At the bottom of the loop, I mark the message as unread (if it was a message I processed).


For Each objMailItem In objRetainerItems
' parse subject and message
' decide if I want to process it
' If message processed
objMailItem.UnRead = True

Next objMailItem

In a countdown loop, I would do this?

For numThisMailItem = 1 to numMail Items
strBody = objRetainerItems.objMailitem(numThisMailItem).Body
next numThisMailItem

Thanks for your help.
 
 
 

VBA not getting all messages

Post by John For » Thu, 20 Nov 2003 04:46:17

Countdown would be "For x = numMailItems to 1 step -1



| I'm parsing the message and inserting data into tables. At the
bottom of the loop, I mark the message as unread (if it was a
message I processed).
|
|
| For Each objMailItem In objRetainerItems
| ' parse subject and message
| ' decide if I want to process it
| ' If message processed
| objMailItem.UnRead = True
|
| Next objMailItem
|
| In a countdown loop, I would do this?
|
| For numThisMailItem = 1 to numMail Items
| strBody = objRetainerItems.objMailitem(numThisMailItem).Body
| next numThisMailItem
|
| Thanks for your help.
 
 
 

VBA not getting all messages

Post by Sue Mosher » Thu, 20 Nov 2003 04:48:36

That's a countup loop. If you are deleting or moving items, you need to count down:

intCount = numMail Items
For i = intCount to 1 Step -1
set objItem = objRetainerItems(i)
' do stuff with objItem
Next

But if you're not deleting or moving, you can use a normal For Each ... Next loop. Don't forget to call Save if you make any changes to the item you're processing. I don't see that in the snippet you posted.
--
Sue Mosher, Outlook MVP
Outlook and Exchange solutions at http://www.yqcomputer.com/
Author of
Microsoft Outlook Programming: Jumpstart
for Administrators, Power Users, and Developers
http://www.yqcomputer.com/
 
 
 

VBA not getting all messages

Post by S2FyZW5 » Thu, 20 Nov 2003 05:31:24

Thanks! I understand! When I set the UnRead property to false, Outlook updates the item Count in my restricted retainer. It is as if I had moved the message.

I need a countdown loop, so Outlook processes from oldest to newest. If I use a countup loop, Outlook will skip a message for each one that I mark UnRead

Thanks, again.
 
 
 

VBA not getting all messages

Post by S2FyZW5 » Thu, 20 Nov 2003 10:28:16

FYI - A final note. Just for grins, I tried a count up loop. It read half of the messages and abended with a subscript out of range error. My objRetainerItems collection got smaller with every message I marked as read

Also, in case it matters to anyone else browsing this thread, the countdown loop starts with the most recent message and proceeds to the oldest message. The countup and original For...Next loops start at the oldest message and progress forward in time.