ResultsEvents_ItemAddEventHandler not fired in cached mode

J

JP

When running Outlook 2007 in cached mode, the
ResultsEvents_ItemAddEventHandler never seems to work. So in the sample
below, "mySearchResults_ItemAdd" never gets called when a "read" item is
added to the search results (filter for AdvancedSearch is
urn:schemas:httpmail:read = 1).

This works find when cached mode is unchecked in Outlook account settings.
Any ideas?

Example Code:

using System;
using System.Windows.Forms;
using Microsoft.VisualStudio.Tools.Applications.Runtime;
using Outlook = Microsoft.Office.Interop.Outlook;
using Microsoft.Office.Interop.Outlook;
using Office = Microsoft.Office.Core;

namespace OutlookAddIn
{

public partial class ThisAddIn
{
private Outlook.Application applicationObject;
private Outlook.Results mySearchResults;

private const string SearchResultsTag =
"READ_MESSAGE_SEARCH_RESULTS_TEST";

private void ThisAddIn_Startup(object sender, System.EventArgs e)
{
ThisAddIn thisAddIn = (ThisAddIn)sender;
applicationObject = (Outlook.Application)thisAddIn.Application;
string strFolderList = "Inbox";
string strFilter = "urn:schemas:httpmail:read = 1";


((Outlook.ApplicationEvents_11_Event)applicationObject).AdvancedSearchComplete +=
new
ApplicationEvents_11_AdvancedSearchCompleteEventHandler(applicationObject_AdvancedSearchComplete);
applicationObject.AdvancedSearch(strFolderList, strFilter, true,
SearchResultsTag);
}

private void ThisAddIn_Shutdown(object sender, System.EventArgs e)
{

}

private void applicationObject_AdvancedSearchComplete(Search
SearchObject)
{
if (SearchObject.Tag == SearchResultsTag)
{
this.mySearchResults = SearchObject.Results;
this.mySearchResults.ItemAdd += new
ResultsEvents_ItemAddEventHandler(mySearchResults_ItemAdd);
}
}

private void mySearchResults_ItemAdd(object Item)
{
// this never gets called when outlook 2007 runs in cached mode
}


#region VSTO generated code

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InternalStartup()
{
this.Startup += new System.EventHandler(ThisAddIn_Startup);
this.Shutdown += new System.EventHandler(ThisAddIn_Shutdown);
}

#endregion
}
}
 
K

Ken Slovak - [MVP - Outlook]

I'll check it out tomorrow on a machine with Outlook 2007 and VSTO 2005SE
installed and let you know what I find.
 
K

Ken Slovak - [MVP - Outlook]

When I cleaned up your code and ran it on Outlook 2007 I got the ItemAdd
handler running in non-cached mode only when Outlook was being exited, not
while it was running. I never did get the event when running in cached mode.

I don't understand what it is you're trying to do. If I changed your code to
add an Outlook.Items object at class level and then added an ItemAdd handler
for Inbox.Items using my Outlook.Items items object the ItemAdd handler ran
as soon as an item was added to the Inbox in both cached and non-cached
mode.

So why are you trying to add the event handler in the way you're doing it? I
could see maybe the utility if the search was on multiple folders, but in
that case I'd probably still use a folder.Items.ItemAdd handler for each
folder and not try to overload that onto the Results collection event
handlers.

BTW, the same problem also occurred in Outlook 2003 using cached mode as
with Outlook 2007.
 
J

JP

Thanks for your response!

I was running the search against multiple folders. I just tried to simplify
it for my example. I was trying to use a method that would allow us to see
any items that went from a unread to a read state within the folder(s)
sepcified in the search.

I will try to use Folder.Items.ItemAdd but I'm not sure if that will allow
me to get events for emails that already exist in a folder and move from a
"unread" to a "read" state.
 
K

Ken Slovak - [MVP - Outlook]

No, in that case I'd be using an ItemChange handler, that would reflect any
changes in existing items. For both new and changed items I'd use both
ItemAdd and ItemChange.

In fact, you can use multiple folders if you do something like getting each
item in Results and finding item.Parent (the parent folder). Then set up
handlers for those folders based on the Results collection, just making sure
you don't duplicate handlers for any one folder.
 
J

JP

I guess I could use the ItemChange event for that and look for items that
change from unread to read?

Thanks again, I really appreciate it.
 
K

Ken Slovak - [MVP - Outlook]

Yes, you could do that. ItemChange passes a reference to the item that was
changed so it would be trivial to check the status of the Unread property in
that handler.
 
J

JP

I guess the Results item add event handler not working in cached mode is a bug.

Anyway, I went with the Folder.Items.ItemChange event and that works well.
Thanks again. You are the kimosabee of Office.
 
K

Ken Slovak - [MVP - Outlook]

Cached mode has a lot of little glitches. I was just discussing another one
in an email exchange with Dmitry, PR_USER_NAME isn't available in cached
mode but is available in online mode.

There are all sorts of other glitches like that. You really in a way have to
think of cached mode as offline mode, since it shares many of the same
differences as offline mode as opposed to online mode.

The bottom line is that you have to select methods/properties/etc. that work
no matter what mode you're in and not try to rely on what's there in online
mode.
 
J

JP

Update:

While we were working with Outlook support, a colleague of mine noticed
that if you reference the Search objects Results collection in the
AdvancedSearch callback, then the ItemAddEventHandler will fire when items
are added to the results. If you do not reference this collection, the
problem will persist. Strange, but it seems to work. There must be some type
of reference count or other strange problem occuring. Anyway, this seems to
be a good workaround in them mean time.

private void applicationObject_AdvancedSearchComplete(Search
SearchObject)
{
if (SearchObject.Tag == SearchResultsTag)
{
this.mySearchResults = SearchObject.Results;
this.mySearchResults.ItemAdd += new
ResultsEvents_ItemAddEventHandler(mySearchResults_ItemAdd);
}


foreach (object item in mySearchResults)
{
// reference results here
}

}
 
K

Ken Slovak - [MVP - Outlook]

Are you expecting the SearchResults.ItemAdd() handler to fire outside the
scope of the AdvancedSearchComplete() event handler? It makes sense that if
you keep an object at a scope where it won't get garbage collected the event
would remain in scope. If that's the case I'm sort of surprised it would
work in any mode. Possibly a difference in when the object is being garbage
collected.
 

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