Strange behavior in Outlook object model

P

Pete Davis

I've got several things that are throwing me off and I could use some help.

I've written a plugin in C#. The plugin aspect works fine.

When I receive a new e-mail, I would like to find each new e-mail. I'm not
sure what the best way to do it is, but what I've resorted to (and I don't
like it) is to try to iterate through all the folders and find every e-mail
since the last time I checked. I then perform some stuff on each e-mail.

There are several issues here:

GetFirst and GetNext: I can't use the foreach() construct in C# because
there is no enumerator. The next thing I tried was something like this:

Outlook.MailItem mi = null;
for (int index = 0; index < folders.Items.Count; index++)
{
mi = (Outlook.MailItem) folders.Items.Item(index);
}

This, however, throws an exception, even though the items are MailItems.

I then tried this:

Outlook.MailItem mi = (Outlook.MailItem) folder.Items.GetFirst()
while (null != mi)
{
...
mi = (Outlook.MailItem) folder.Items.GetNext()
}

Except in this case, GetNext() always seems to return the first item. What's
up with that? Isn't it supposed to go to the next item?


My second problem has to do with getting the time from the messages.

If I do:

DateTime dt = mi.SentOn;
or
DateTime dt = mi.ReceivedTime;

I always get 6/20/2002 7:32:10PM

I'm real close to accomplishing what I want to do, if I can just get this
crap ironed out. Thanks.

Pete
 
D

Dmitry Streblechenko

1. Are you sure they all are mailitems? How about NDRs, etc?
The reason why GetNext does not work for you is because every time you call
it, your code retrieves a new instance of the Items object. Store the Items
object in a local variable to make sure you are always using the same
instance:

Outlook.Items Items= folder.Items;
Outlook.MailItem mi = Items.GetFirst()
while (null != mi)
{
...
mi = (Outlook.MailItem)Items.GetNext()
}

2. See #1, you were probably always getting the same item.


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

Pete Davis

Thanks, that was it. I guess it makes sense that it needs to keep its
placeholder in the local copy.

Pete
 

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