Type of object for AdvancedSearch

R

Renjith

Hello
When I do advanced search using the AdvancedSearch method, I get the results
in the AdvancedSearch complete method. I am using C# to do this. But I have
problem here. I am not able to get the type of the item that was returned in
the result. For example it can be a MailItem or ContactItem. If I cast it to
a mail item it will result in an exception. How can I find the type of the
item before casting it?
The code is given below.
private void outLookApp_AdvancedSearchComplete(Search searchObj)
{
applicationObject.AdvancedSearchComplete -= new
ApplicationEvents_11_AdvancedSearchCompleteEventHandler (outLookApp_AdvancedSearchComplete);

string strZipFile = Path.GetTempFileName() + ".zip";
string[] strTempFiles = new string[searchObj.Results.Count];
for( int nCount = 0; nCount < searchObj.Results.Count; nCount++)
{
MailItem item = (MailItem)searchObj.Results[nCount+1]; //This will throw
an exception if the item is not a MailItem
string strMailPath = Path.GetTempFileName() + ".msg";
item.SaveAs(strMailPath, OlSaveAsType.olMSG);
Helper.ZipFile(strMailPath,strZipFile);
strTempFiles[nCount] = strMailPath;
}
}
 
S

Sue Mosher [MVP-Outlook]

I'm not a C# programmer, but my understanding is that the right approach in C# is to this kind of statement witih the Is operator to test for each object class

if (Item is Ol.MailItem)

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
K

Ken Slovak - [MVP - Outlook]

Or a Try...Catch block where if one item type cast fails another can be
tried.




I'm not a C# programmer, but my understanding is that the right approach in
C# is to this kind of statement witih the Is operator to test for each
object class

if (Item is Ol.MailItem)

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers
 
R

Renjith

thankyou for the hint, actually I have now the try catch blocks for each
type, but the code becomes a bit lengthy with 16 such blocks :)

Ken Slovak - said:
Or a Try...Catch block where if one item type cast fails another can be
tried.




I'm not a C# programmer, but my understanding is that the right approach in
C# is to this kind of statement witih the Is operator to test for each
object class

if (Item is Ol.MailItem)

--
Sue Mosher, Outlook MVP
Author of Configuring Microsoft Outlook 2003

and Microsoft Outlook Programming - Jumpstart for
Administrators, Power Users, and Developers


Renjith said:
Hello
When I do advanced search using the AdvancedSearch method, I get the
results
in the AdvancedSearch complete method. I am using C# to do this. But I
have
problem here. I am not able to get the type of the item that was returned
in
the result. For example it can be a MailItem or ContactItem. If I cast it
to
a mail item it will result in an exception. How can I find the type of the
item before casting it?
The code is given below.
private void outLookApp_AdvancedSearchComplete(Search searchObj)
{
applicationObject.AdvancedSearchComplete -= new
ApplicationEvents_11_AdvancedSearchCompleteEventHandler
(outLookApp_AdvancedSearchComplete);

string strZipFile = Path.GetTempFileName() + ".zip";
string[] strTempFiles = new string[searchObj.Results.Count];
for( int nCount = 0; nCount < searchObj.Results.Count; nCount++)
{
MailItem item = (MailItem)searchObj.Results[nCount+1]; //This will throw
an exception if the item is not a MailItem
string strMailPath = Path.GetTempFileName() + ".msg";
item.SaveAs(strMailPath, OlSaveAsType.olMSG);
Helper.ZipFile(strMailPath,strZipFile);
strTempFiles[nCount] = strMailPath;
}
}
 
K

Ken Slovak - [MVP - Outlook]

It probably could be cut down by casting to a an object of indeterminate
type and testing the .Class property of the object and then casting it to
the correct type, all within a try...catch to handle any errors.
 

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