Hello there, I've been doing some research about the Outlook.Selection interface and made an application out of it. My goal is to get all of the items in my inbox folder. I have already created a way to get all of them, my problem is to filter each types of items in my inbox. I found a link that could handle it but it is written in VB. It has all of the information that I needed but it seems that I can't get the MessageClass of the variable which is of type Object. Here is the link: http://support.microsoft.com/kb/240935

This is exactly what I want my filtering to be in C#. Any thoughts and suggestions I would really appreciate. Thank you.

Accessing mails from outlooks inbox is generally quite simple and mine seems to pick up the differences. What code do you have as while I didnt derrive mine from that vb, mine uses pretty much the same concept.

Thanks for the reply. I'm using VSTO Sir. My only problem right now is to filter the items that I'm dragging (whether it is a MailItem, MeetingItem, AppointmenItem etc.). The sample code in the link uses MessageClass which he got from the variable of type Object which contains the item selected in the ActiveExplorer. I think C# has a different way of getting the MessageClass which is I'm not that really familiar.

Frirstly, Im not male. Please do not make such rash assumptions.

Secondly, Ive asked for some code as it would seem you've approached it very differently from me as all items correctly report their code for me, and perhaps you're going about it the wrong way.

I'm so sorry, I didn't mean it. This is my test program seperated from my application.

private void Form1_Load(object sender, EventArgs e)
 {
  Outlook.Application app = new Outlook.Application();
            Outlook.NameSpace nmspc = app.GetNamespace("mapi");
            Outlook.MAPIFolder inbox = nmspc.GetDefaultFolder(Microsoft.Office.Interop.Outlook.OlDefaultFolders.olFolderInbox);

          
            Outlook.MeetingItem item;
            for (int i = 8; i < 9; i++)
           {
                item = (Outlook.MeetingItem)inbox.Items[i];
                item.SaveAs(@"c:\TempFile\Temp" + item.EntryID.ToString() + ".msg", Outlook.OlSaveAsType.olMSG);
                MessageBox.Show(item.EntryID.ToString());
            }
           
 }

This is just a test sample because I don't want to mess the whole application that I made. This is my way of getting all the items in my inbox. All I need is a filtering to determine if it is a MailItem, ApplicationItem, MeetingItem etc.

OK, but that will pick up only meeting items, as you cant assign mail items to a meetingitem, it will cause an exception. the messageclass property is text and will confirm then which subtype of the item you picked is, such as out of office etc.

I agree that MessageClass property will confirm which subtype is picked but I'm having a problem how to use it in C#.

Its a string, it will give you in words the type.

PS - just like in the vb example you posted

The line

strMessageClass = oItem.MessageClass

seems does not work in C#. oItem is of type Object. It throws an error.

In your code above, "Outlook.MeetingItem item" as long as you called item.MessageClass you would be ok.

This is what I meant

Outlook.Application app = new Outlook.Application();
Outlook.Selection oSel;
Outlook.Explorer oEx = app.ActiveExplorer;

oSel = oEx.Selection;

Object oItem;

oItem = oSel.Item;

String msgClass = oItem.MessageClass;

I want to know the item selected to filter if it is the item that I want. But it seems the line in red it gives me an error.

Because (DUH) Object is not an outlook class that has that property.

That is what the link exactly did only in VB. I think there is a way in C# but can't just find it.

But VB is lazy and allows you to do hiddeous things like adding characters and integers and setting strings to numbers if you let it. That doesnt make it right.

if you declare object as o you could test if o is Outlook.MailItem that seems to work.

Thanks for the reply, can you show a snippet how to implement?

Good god, no offense but that sounds as lazy as it gets

Outlook.Application app = new Outlook.Application();
Outlook.Selection oSel;
Outlook.Explorer oEx = app.ActiveExplorer;

oSel = oEx.Selection;

Object oItem;

oItem = oSel.Item;
if (oItem is Outlook.MailItem)
{
  Outlook.MailItem oMail = (Outlook.MailItem)Oitem;
  label1.Text = oMail.Subject;
}

all you had to do was paste in the info given..

I now understand, thank you. I'm sorry for letting you post a snippet because I cannot understand well what you mean. Thanks again.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.