AdvancedSearchComplete Search param not immediately ready for use

S

sb

Hello,

I am developing an Outlook add-in using VSTO in VS2008, using .Net 2.0 and
Outlook 2003. I have a bit of functionality that executes an advancedsearch
and catches the AdvancedSearchComplete event. However, when the results come
back, the Search parameter passed to the event handler does not appear to be
ready for use.

The search:

sFilter = "\"urn:schemas:httpmail:subject\" LIKE '%subject
string%' AND ";
sFilter += "\"urn:schemas:httpmail:textdescription\" LIKE '%text
description%'";

//Execute the search
try
{
Outlook.Application a = m_addIn.Application;

sScope = "'\\\\valid folder\\'";
a.AdvancedSearch(sScope, sFilter, true, "Tag");
}


The event handler:

private void m_outlook_AdvancedSearchComplete(Outlook.Search
searchObj)
{
//MessageBox.Show(searchObj.Results.Count.ToString());


for (int i = 1; i <= searchObj.Results.Count; i++)
{
//do stuff
}


Everything seems to be working fine, the search is successful and the
AdvancedSearchComplete event fires when it is done. However, when
AdvancedSearchComplete is fired as expected, searchObj.Results.Count is
ALWAYS zero. But... If that MessageBox.Show line is uncommented, it always
displays '0', but then the subsequent calls to searchObj in the for... and
beyond correctly reflect the search Results. I have tried a Thread.Sleep()
call in there to no avail.

Thoughts? I thought the results were guaranteed to be ready when this event
fires. Is there some setting I am missing somewhere?

thanks
 
K

Ken Slovak - [MVP - Outlook]

I can't repro that here using VS 2008, C# and Outlook 2003. I used this
search as a test:

Outlook.Search search = _outlook.AdvancedSearch("Inbox",
"urn:schemas:mailheader:subject = 'Checking in'", true, "Foo");

When the event fired the MessageBox showed 1 result in my Inbox, as
expected.

If you want to loop the Results collection you will need to use
GetFirst()/GetLast() or get an IEnumerator object to iterate the collection
that way.
 
S

sb

Thank you for your response, Ken.

It seems strange it only happens for me. Are you using .Net 2.0 (as opposed
to the VS2008 default of 3.5)? Also, I am running on Win 7 RC, and the email
accounts in my test config of Outlook are just a couple of hotmail addresses
using Outlook Connector. Not sure why any of these would make any difference.

In any event, I finally just solved this problem with an
Application.DoEvents() call in the event handler. Weird.

Forgive me, I am no Outlook extensibility master. While
GetFirst()/GetNext() may be better in some way, why would a for() loop not
work to iterate the results?

thanks
 
K

Ken Slovak - [MVP - Outlook]

I did the test in an addin I'm working on which happens to use Framework 2.0
and not 3.5. The code was tested on WinXP SP3 and Outlook 2003 SP3.

I'd guess that the connector and hotmail wouldn't make a difference, the
search is being done on a specific folder or folders. I don't use the
connector, are the folders you were searching local in a PST file? If so
then there shouldn't be a difference. I happened to test using a cached
Exchange mailbox.

I wonder if using Application.AdvancedSearch() as a function, as I did might
make a difference. You might try that and see.

I suggested using the Get* functions rather than something like
searchObject just because you don't have to deal with Outlook collections
starting with Item[1] and not Item[0]. One less thing to remember. Not a big
deal, I often use a for loop like yours.
 
S

sb

Ken Slovak - said:
I suggested using the Get* functions rather than something like
searchObject just because you don't have to deal with Outlook collections
starting with Item[1] and not Item[0]. One less thing to remember. Not a big
deal, I often use a for loop like yours.


Ah yes - I already encountered and passed that annoyance. <g>

Thanks again for your 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