Find Related Messages in Outlook 2003 with VSTO 2005 C#




I have a sample code here which simply looks for the same email subject and
returns the total of number messages located in a folder. But this code took
noticeably longer time to execute compare to the good old Outlook's "Find All
/ Related Messages" feature. This Outlook's feature returns a list in an
instant without delay while this sample code took at least 2 or more seconds
to return just a number.

==================== begin ==================
private int FindEmailsWithSameSubject(string EmailSubject)
int MessageFound = 0;

// The following two variables are declared
// private Outlook.Application App;
// private Outlook.Explorer CurrentExplorer;
// and defined globally in the main class:
// App = new Outlook.Application();
// CurrentExplorer = App.ActiveExplorer();

Outlook.MAPIFolder SelectedFolder = CurrentExplorer.CurrentFolder;

foreach (Object SelectedObject in SelectedFolder.Items)
// Only work on messages which are email items.
if (SelectedObject is Outlook.MailItem)
Outlook.MailItem MailItem = SelectedObject as Outlook.MailItem;
if (EmailSubject == MailItem.Subject)
return MessageFound;
==================== end ==================

This code is executed on an inbox with just 190 messages. Questions:

1) Why this code take a longer time to run?
2) Is there a better way to find all related messages?

Thanks a lot.

Ken Slovak - [MVP - Outlook]

It would be much faster to use a filter or restriction on the Items
collection of that folder using Subject as the filter condition. Review the
help in the Outlook VBA Object Browser for Items.Restrict, it shows how to
construct a restriction on various Outlook properties. The same type filter
also would apply if you use the Find methods. Which you use depends on how
many hits you expect.
Apr 22, 2013
Reaction score
Hi Ken,

Can you please show me the sample code on how i can find related message either using find or restric method.

I am able to filter using restrict method and got all the mails in a object satisfying the filter/restric criteria but I am not able find a way to display them in the exact same manner how we see in outlook when we use filter or find related mail option

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