C# Outlook problem

J

Joseph Kormann

I'm trying to tie my C# application into the local running Outlook
application
through the Interop.Outlook.DLL. I'm looking for new / unread messages in
the Inbox. No problem getting into the Inbox, but there is a problem using
the Outlook.Items.Restrict method. The returned ItemsClass shows 10 unread
messages (correct), but cycling through the list only shows me 6 of them.
When I run it again, I get 4 unread messages (correct), but going through
the list only show 3 of them. Evenually I get to 1 unread message which is
processed, but this requires me to run the outer loop (which goes to the
Inbox) multiple times.


Code:
string strRestrict = "[Unread] = false";

ItemsClass unreadMessages =
(ItemsClass)mInboxFolder.Items.Restrict(strRestrict);

unreadMessages.Sort("[SentOn]", true);

Console.WriteLine("# of unread messages: " + unreadMessages.Count);


MailItemClass mailItem = null;

mailItem = (MailItemClass)unreadMessages.GetFirst();

while (mailItem != null)
{

Console.WriteLine(mailItem.Subject);

mailItem.UnRead = true;

mailItem = (MailItemClass)unreadMessages.GetNext();

Console.WriteLine("\t unread: " + unreadMessages.Count);

}


As a test, I changed the above 'while' to "while (unreadMessages.Count >
0)", but then I get a thrown exception that the object reference is not set.

Any help?
 

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