I am able to extract mail's subject from my outlook root folder.
Outlook.Application test_app = new Outlook.Application();
Outlook.NameSpace test_space = test_app.GetNamespace("MAPI");
Outlook.MAPIFolder test_folder = test_space.Folders["TanRong.Loo@infineon.com"].Folders["Test"];
foreach (Outlook.MailItem items in test_folder.Items)
{
Console.WriteLine(items.Subject);
}
However, when I try to access the subfolder of my root "Test" folder, it gives me an error message.
Outlook.Application test_app = new Outlook.Application();
Outlook.NameSpace test_space = test_app.GetNamespace("MAPI");
Outlook.MAPIFolder test_folder = test_space.Folders["TanRong.Loo@infineon.com"].Folders["Test"].Folders["Test1"];
foreach (Outlook.MailItem items in test_folder.Items)
{
Console.WriteLine(items.Subject);
}
The error message is:
Unhandled Exception: System.InvalidCastException: Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.Outlook.MailItem'.
This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063034-0000-0000-C000-000000000046}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
at outlook_to_excel.Program.Main(String[] args) in C:\Users\LooTanRo\Desktop\Outlook Export\CS\outlook_to_excel\outlook_to_excel\Program.cs:line 51
What did i do wrong? Please help me, thanks in advance.
You are assuming you only have MailItem
objects in the folder. You can also have ReportItem
or MeetingItem
objects.
Change the loop to
foreach (object item in test_folder.Items)
{
Outlook.MailItem mItem = item as Outlook.MailItem;
if (mItem != null)
{
...
User contributions licensed under CC BY-SA 3.0