Cannot access MailItems after using GetFolderFromID

Joined
Nov 4, 2005
Messages
1
Reaction score
0
Hello

I access one subfolder inside the public folders in Outlook 2003. I use the GetFolderFromID method and all 14 items (mailitems) within it are returned succesfully. I then try to iterate and add their data to a dataset, unsuccesfully. Here is my code

Code:
[size=2][color=#0000ff]public[/color][/size][size=2] DataSet getPublicFolderDataSet([/size][size=2][color=#0000ff]string[/color][/size][size=2] s)
 
{
 
Outlook.MailItem item;
 
 
 
DataSet rv = [/size][size=2][color=#0000ff]new[/color][/size][size=2] DataSet();
 
rv.DataSetName = "PublicFolder";
 
rv.Tables.Add("Public Folder: " + s);
 
rv.Tables[0].Columns.Add("From");
 
rv.Tables[0].Columns.Add("To");
 
rv.Tables[0].Columns.Add("Cc");
 
rv.Tables[0].Columns.Add("Subject");
 
rv.Tables[0].Columns.Add("Received");
 
rv.Tables[0].Columns.Add("Message");
 
[/size][size=2][color=#0000ff]try
 
[/color][/size][size=2]{
 
[/size][size=2][color=#0000ff]string[/color][/size][size=2][color=#0000ff]value[/color][/size][size=2] = tbl[s].ToString();
 
[/size][size=2][color=#0000ff]int[/color][/size][size=2] slashPosition = [/size][size=2][color=#0000ff]value[/color][/size][size=2].IndexOf("/");
 
[/size][size=2][color=#0000ff]string[/color][/size][size=2] EntryID = [/size][size=2][color=#0000ff]value[/color][/size][size=2].Substring(0,slashPosition);
 
[/size][size=2][color=#0000ff]string[/color][/size][size=2] StoreID = [/size][size=2][color=#0000ff]value[/color][/size][size=2].Substring(slashPosition + 1);
 
Outlook.MAPIFolder oMyFolder = objNamespace.GetFolderFromID(EntryID,StoreID);
 
System.Collections.IEnumerator MsgEnum = oMyFolder.Items.GetEnumerator();
 
 
 
[/size][size=2][color=#0000ff]while[/color][/size][size=2] (MsgEnum.MoveNext()) 
 
{
 
item = (Outlook.MailItem)MsgEnum.Current;
 
rv.Tables[0].Rows.Add([/size][size=2][color=#0000ff]new[/color][/size][size=2][color=#0000ff]object[/color][/size][size=2][] { item.SenderName,
 
item.To,
 
item.CC,
 
item.Subject,
 
item.ReceivedTime,
 
item.Body
 
});
 
}
 
}
 
[/size][size=2][color=#0000ff]catch[/color][/size][size=2] (System.Exception e)
 
{
 
Console.WriteLine(e);
 
}
 
[/size][size=2][color=#0000ff]return[/color][/size][size=2] rv;
 
}
 
[/size]

On this line
Code:
item = (Outlook.MailItem)MsgEnum.Current;

I get an exception "Specified cast is not valid". The weird thing is that i use the same cast when i want to iterate through one of my default folders, such as Inbox. Have you got any idea why is this might happening?

Thanks, Kostas
 
Top