visual c# advancedsearch does not return results or will not fire

  • Thread starter Thread starter matt
  • Start date Start date
M

matt

Please advise where this maybe best directed, since I really don't know vba
well enough to help assist with learning C#.
Also first time w/ Outlook com object.


these are the only examples using advancedsearch w/ C# I have found online:
http://www.devnewsgroups.net/group/microsoft.public.exchange.development/topic56189.aspx
http://showmeself.spaces.live.com/?...ogview&_c=BlogPart&partqs=amonth=5&ayear=2007

I have tried this a few ways, but here is code as I have it now.
Would prefer to be able to pass the path as referenced below - which I was
able to do it seems - but changed method and changed path back to "inbox"
to be sure that wasn't the issue.
Please help, goal is to get item (mail) count using advanced find to search
subfolders.


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Reflection;

using System.Collections;

using System.Runtime.InteropServices;





namespace outlook_TLE_Reader

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

public void button1_Click(object sender, EventArgs e)

{

// ThisAdvancedSearchComplete();

// findItems();

Test3();

}

public void Test3()

{

_ThisAdvancedSearchComplete();

}



private Outlook.Folders GetFolders()

{

throw new Exception("The method or operation is not implemented.");

}



// Below Start test 3 here
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// void _ThisAdvancedSearchComplete(object sender, EventArgs e)

public void _ThisAdvancedSearchComplete()


{

Outlook.Application olapp_1 = new Outlook.Application();

Outlook.NameSpace ns = olapp_1.GetNamespace("MAPI");

ns.Logon(Missing.Value,Missing.Value,false,true);

Outlook.MAPIFolder TL_Folder = ns.Application.Session.Folders[2];

int count = TL_Folder.Items.Count;

// set up search parameters

// string path = ns.Application.Session.Folders[2].FolderPath;


string path = TL_Folder.FolderPath;

// string Scope = "'" + path + "'";

string Scope = @"Inbox";

// string filter = "urn:schemas:mailheader:subject = '%a%'";

string filter = @"""subject"" LIKE '%a%'";

//string filter = @"urn:schemas:httpmail:msgfolderroot:kind = '%'";

object subfolders = true;

string tag = "SearchForMail";

// const string tag = @"SearchForMail";

try

{


olapp_1.AdvancedSearchComplete += new
Outlook.ApplicationEvents_11_AdvancedSearchCompleteEventHandler(olapp_1_AdvancedSearchComplete);


//Outlook.Search SearchObject = olapp_1.AdvancedSearch(Scope, filter,
subfolders, tag);

label2.Text = path.ToString();

startAdvancedSearch(olapp_1);

}

catch (System.Runtime.InteropServices.COMException ex)

{

MessageBox.Show(ex.Message + " " + "\\n" + ex.StackTrace);

}

}

void olapp_1_AdvancedSearchComplete(Outlook.Search SearchObject)


{

if (SearchObject != null)

{

MessageBox.Show(SearchObject.Results.Count.ToString());

}

//throw new Exception("The method or operation is not implemented.");

}

private static void startAdvancedSearch(Outlook._Application olapp_1)

{

// string Scope = "'" + path + "'";

string Scope = @"Inbox";

// string filter = "urn:schemas:mailheader:subject = '%a%'";

string filter = @"""subject"" LIKE '%a%'";

//string filter = @"urn:schemas:httpmail:msgfolderroot:kind = '%'";

object subfolders = true;

string tag = "SearchForMail";

// const string tag = @"SearchForMail";

Outlook.Search SearchObject = olapp_1.AdvancedSearch(Scope, filter,
subfolders, tag);

}





// End test 3
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



private void label2_Click(object sender, EventArgs e)

{


}

private void label1_Click(object sender, EventArgs e)

{


}

}

}
 
The main problem is probably with your search string. I don't write C#, so you'll have to build the string yourself, but the final result to search for the letter "a" in the subject would be:

"urn:schemas:mailheader:subject" Like '%a%'

Note that the property name needs quotation marks around it.

The easiest way to see the correct syntax, BTW, is to filter a view and then look at the view's SQL tab.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


matt said:
Please advise where this maybe best directed, since I really don't know vba
well enough to help assist with learning C#.
Also first time w/ Outlook com object.


these are the only examples using advancedsearch w/ C# I have found online:
http://www.devnewsgroups.net/group/microsoft.public.exchange.development/topic56189.aspx
http://showmeself.spaces.live.com/?...ogview&_c=BlogPart&partqs=amonth=5&ayear=2007

I have tried this a few ways, but here is code as I have it now.
Would prefer to be able to pass the path as referenced below - which I was
able to do it seems - but changed method and changed path back to "inbox"
to be sure that wasn't the issue.
Please help, goal is to get item (mail) count using advanced find to search
subfolders.


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Reflection;

using System.Collections;

using System.Runtime.InteropServices;





namespace outlook_TLE_Reader

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

public void button1_Click(object sender, EventArgs e)

{

// ThisAdvancedSearchComplete();

// findItems();

Test3();

}

public void Test3()

{

_ThisAdvancedSearchComplete();

}



private Outlook.Folders GetFolders()

{

throw new Exception("The method or operation is not implemented.");

}



// Below Start test 3 here
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// void _ThisAdvancedSearchComplete(object sender, EventArgs e)

public void _ThisAdvancedSearchComplete()


{

Outlook.Application olapp_1 = new Outlook.Application();

Outlook.NameSpace ns = olapp_1.GetNamespace("MAPI");

ns.Logon(Missing.Value,Missing.Value,false,true);

Outlook.MAPIFolder TL_Folder = ns.Application.Session.Folders[2];

int count = TL_Folder.Items.Count;

// set up search parameters

// string path = ns.Application.Session.Folders[2].FolderPath;


string path = TL_Folder.FolderPath;

// string Scope = "'" + path + "'";

string Scope = @"Inbox";

// string filter = "urn:schemas:mailheader:subject = '%a%'";

string filter = @"""subject"" LIKE '%a%'";

//string filter = @"urn:schemas:httpmail:msgfolderroot:kind = '%'";

object subfolders = true;

string tag = "SearchForMail";

// const string tag = @"SearchForMail";

try

{


olapp_1.AdvancedSearchComplete += new
Outlook.ApplicationEvents_11_AdvancedSearchCompleteEventHandler(olapp_1_AdvancedSearchComplete);


//Outlook.Search SearchObject = olapp_1.AdvancedSearch(Scope, filter,
subfolders, tag);

label2.Text = path.ToString();

startAdvancedSearch(olapp_1);

}

catch (System.Runtime.InteropServices.COMException ex)

{

MessageBox.Show(ex.Message + " " + "\\n" + ex.StackTrace);

}

}

void olapp_1_AdvancedSearchComplete(Outlook.Search SearchObject)


{

if (SearchObject != null)

{

MessageBox.Show(SearchObject.Results.Count.ToString());

}

//throw new Exception("The method or operation is not implemented.");

}

private static void startAdvancedSearch(Outlook._Application olapp_1)

{

// string Scope = "'" + path + "'";

string Scope = @"Inbox";

// string filter = "urn:schemas:mailheader:subject = '%a%'";

string filter = @"""subject"" LIKE '%a%'";

//string filter = @"urn:schemas:httpmail:msgfolderroot:kind = '%'";

object subfolders = true;

string tag = "SearchForMail";

// const string tag = @"SearchForMail";

Outlook.Search SearchObject = olapp_1.AdvancedSearch(Scope, filter,
subfolders, tag);

}





// End test 3
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



private void label2_Click(object sender, EventArgs e)

{


}

private void label1_Click(object sender, EventArgs e)

{


}

}

}
 
Yeah, that did the trick, great to know what I was overlooking there.
Definitely, thanks for pin-pointing that.

Now I need to return all items, regarless of subject or mail type - calendar
item even.

It may be an emtpy subject field will get me that - but in outlook you must
specify any outlook item when doing an advanced search through the gui.

But if it works, I still don't know what to pass - wild card - to catch
everything. Even still, probably another approach needed to return what I
want anyway.

You know of a webdav clause that would return all items?



The main problem is probably with your search string. I don't write C#, so
you'll have to build the string yourself, but the final result to search for
the letter "a" in the subject would be:

"urn:schemas:mailheader:subject" Like '%a%'

Note that the property name needs quotation marks around it.

The easiest way to see the correct syntax, BTW, is to filter a view and then
look at the view's SQL tab.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


matt said:
Please advise where this maybe best directed, since I really don't know
vba
well enough to help assist with learning C#.
Also first time w/ Outlook com object.


these are the only examples using advancedsearch w/ C# I have found
online:
http://www.devnewsgroups.net/group/microsoft.public.exchange.development/topic56189.aspx
http://showmeself.spaces.live.com/?...ogview&_c=BlogPart&partqs=amonth=5&ayear=2007

I have tried this a few ways, but here is code as I have it now.
Would prefer to be able to pass the path as referenced below - which I was
able to do it seems - but changed method and changed path back to "inbox"
to be sure that wasn't the issue.
Please help, goal is to get item (mail) count using advanced find to
search
subfolders.


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Reflection;

using System.Collections;

using System.Runtime.InteropServices;





namespace outlook_TLE_Reader

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

public void button1_Click(object sender, EventArgs e)

{

// ThisAdvancedSearchComplete();

// findItems();

Test3();

}

public void Test3()

{

_ThisAdvancedSearchComplete();

}



private Outlook.Folders GetFolders()

{

throw new Exception("The method or operation is not implemented.");

}



// Below Start test 3 here
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// void _ThisAdvancedSearchComplete(object sender, EventArgs e)

public void _ThisAdvancedSearchComplete()


{

Outlook.Application olapp_1 = new Outlook.Application();

Outlook.NameSpace ns = olapp_1.GetNamespace("MAPI");

ns.Logon(Missing.Value,Missing.Value,false,true);

Outlook.MAPIFolder TL_Folder = ns.Application.Session.Folders[2];

int count = TL_Folder.Items.Count;

// set up search parameters

// string path = ns.Application.Session.Folders[2].FolderPath;


string path = TL_Folder.FolderPath;

// string Scope = "'" + path + "'";

string Scope = @"Inbox";

// string filter = "urn:schemas:mailheader:subject = '%a%'";

string filter = @"""subject"" LIKE '%a%'";

//string filter = @"urn:schemas:httpmail:msgfolderroot:kind = '%'";

object subfolders = true;

string tag = "SearchForMail";

// const string tag = @"SearchForMail";

try

{


olapp_1.AdvancedSearchComplete += new
Outlook.ApplicationEvents_11_AdvancedSearchCompleteEventHandler(olapp_1_AdvancedSearchComplete);


//Outlook.Search SearchObject = olapp_1.AdvancedSearch(Scope, filter,
subfolders, tag);

label2.Text = path.ToString();

startAdvancedSearch(olapp_1);

}

catch (System.Runtime.InteropServices.COMException ex)

{

MessageBox.Show(ex.Message + " " + "\\n" + ex.StackTrace);

}

}

void olapp_1_AdvancedSearchComplete(Outlook.Search SearchObject)


{

if (SearchObject != null)

{

MessageBox.Show(SearchObject.Results.Count.ToString());

}

//throw new Exception("The method or operation is not implemented.");

}

private static void startAdvancedSearch(Outlook._Application olapp_1)

{

// string Scope = "'" + path + "'";

string Scope = @"Inbox";

// string filter = "urn:schemas:mailheader:subject = '%a%'";

string filter = @"""subject"" LIKE '%a%'";

//string filter = @"urn:schemas:httpmail:msgfolderroot:kind = '%'";

object subfolders = true;

string tag = "SearchForMail";

// const string tag = @"SearchForMail";

Outlook.Search SearchObject = olapp_1.AdvancedSearch(Scope, filter,
subfolders, tag);

}





// End test 3
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



private void label2_Click(object sender, EventArgs e)

{


}

private void label1_Click(object sender, EventArgs e)

{


}

}

}
 
nevermind, this is all I needed

string filter = @"";

Also, imediately below, that was a really cool tip, since I never even
thought to do that before.
The easiest way to see the correct syntax, BTW, is to filter a view and
then look at the view's SQL tab.


matt said:
Yeah, that did the trick, great to know what I was overlooking there.
Definitely, thanks for pin-pointing that.

Now I need to return all items, regarless of subject or mail type -
calendar item even.

It may be an emtpy subject field will get me that - but in outlook you
must specify any outlook item when doing an advanced search through the
gui.

But if it works, I still don't know what to pass - wild card - to catch
everything. Even still, probably another approach needed to return what I
want anyway.

You know of a webdav clause that would return all items?



The main problem is probably with your search string. I don't write C#, so
you'll have to build the string yourself, but the final result to search
for the letter "a" in the subject would be:

"urn:schemas:mailheader:subject" Like '%a%'

Note that the property name needs quotation marks around it.

The easiest way to see the correct syntax, BTW, is to filter a view and
then look at the view's SQL tab.

--
Sue Mosher, Outlook MVP
Author of Microsoft Outlook 2007 Programming:
Jumpstart for Power Users and Administrators
http://www.outlookcode.com/article.aspx?id=54


matt said:
Please advise where this maybe best directed, since I really don't know
vba
well enough to help assist with learning C#.
Also first time w/ Outlook com object.


these are the only examples using advancedsearch w/ C# I have found
online:
http://www.devnewsgroups.net/group/microsoft.public.exchange.development/topic56189.aspx
http://showmeself.spaces.live.com/?...ogview&_c=BlogPart&partqs=amonth=5&ayear=2007

I have tried this a few ways, but here is code as I have it now.
Would prefer to be able to pass the path as referenced below - which I
was
able to do it seems - but changed method and changed path back to
"inbox"
to be sure that wasn't the issue.
Please help, goal is to get item (mail) count using advanced find to
search
subfolders.


using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Text;

using System.Windows.Forms;

using System.Reflection;

using System.Collections;

using System.Runtime.InteropServices;





namespace outlook_TLE_Reader

{

public partial class Form1 : Form

{

public Form1()

{

InitializeComponent();

}

public void button1_Click(object sender, EventArgs e)

{

// ThisAdvancedSearchComplete();

// findItems();

Test3();

}

public void Test3()

{

_ThisAdvancedSearchComplete();

}



private Outlook.Folders GetFolders()

{

throw new Exception("The method or operation is not implemented.");

}



// Below Start test 3 here
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////

// void _ThisAdvancedSearchComplete(object sender, EventArgs e)

public void _ThisAdvancedSearchComplete()


{

Outlook.Application olapp_1 = new Outlook.Application();

Outlook.NameSpace ns = olapp_1.GetNamespace("MAPI");

ns.Logon(Missing.Value,Missing.Value,false,true);

Outlook.MAPIFolder TL_Folder = ns.Application.Session.Folders[2];

int count = TL_Folder.Items.Count;

// set up search parameters

// string path = ns.Application.Session.Folders[2].FolderPath;


string path = TL_Folder.FolderPath;

// string Scope = "'" + path + "'";

string Scope = @"Inbox";

// string filter = "urn:schemas:mailheader:subject = '%a%'";

string filter = @"""subject"" LIKE '%a%'";

//string filter = @"urn:schemas:httpmail:msgfolderroot:kind = '%'";

object subfolders = true;

string tag = "SearchForMail";

// const string tag = @"SearchForMail";

try

{


olapp_1.AdvancedSearchComplete += new
Outlook.ApplicationEvents_11_AdvancedSearchCompleteEventHandler(olapp_1_AdvancedSearchComplete);


//Outlook.Search SearchObject = olapp_1.AdvancedSearch(Scope, filter,
subfolders, tag);

label2.Text = path.ToString();

startAdvancedSearch(olapp_1);

}

catch (System.Runtime.InteropServices.COMException ex)

{

MessageBox.Show(ex.Message + " " + "\\n" + ex.StackTrace);

}

}

void olapp_1_AdvancedSearchComplete(Outlook.Search SearchObject)


{

if (SearchObject != null)

{

MessageBox.Show(SearchObject.Results.Count.ToString());

}

//throw new Exception("The method or operation is not implemented.");

}

private static void startAdvancedSearch(Outlook._Application olapp_1)

{

// string Scope = "'" + path + "'";

string Scope = @"Inbox";

// string filter = "urn:schemas:mailheader:subject = '%a%'";

string filter = @"""subject"" LIKE '%a%'";

//string filter = @"urn:schemas:httpmail:msgfolderroot:kind = '%'";

object subfolders = true;

string tag = "SearchForMail";

// const string tag = @"SearchForMail";

Outlook.Search SearchObject = olapp_1.AdvancedSearch(Scope, filter,
subfolders, tag);

}





// End test 3
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



private void label2_Click(object sender, EventArgs e)

{


}

private void label1_Click(object sender, EventArgs e)

{


}

}

}
 
Back
Top