whorsfall
asked on
Convert Outlook data and display into a DataGridView
Hi,
From the code below I would like to work out how to convert the data (an Outlook Table) and display it into a DataGridView. Is there any easy way todo this. I set the DataSource = table but it does not work.
Any ideas. The code below:
From the code below I would like to work out how to convert the data (an Outlook Table) and display it into a DataGridView. Is there any easy way todo this. I set the DataSource = table but it does not work.
Any ideas. The code below:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Outlook = Microsoft.Office.Interop.Outlook;
namespace EmailScan
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Microsoft.Office.Interop.Outlook.Application app = null;
Microsoft.Office.Interop.Outlook._NameSpace ns = null;
app = new Microsoft.Office.Interop.Outlook.Application();
ns = app.GetNamespace("MAPI");
ns.Logon(null, null, false, false);
// Obtain Inbox
Outlook.Folder folder =
ns.GetDefaultFolder(
Outlook.OlDefaultFolders.olFolderInbox)
as Outlook.Folder;
// Create filter
string filter = "";
Outlook.Table table =
folder.GetTable(filter,
Outlook.OlTableContents.olUserItems);
// Remove default columns
table.Columns.RemoveAll();
// Add using built-in name
table.Columns.Add("EntryID");
table.Columns.Add("Subject");
table.Columns.Add("ReceivedTime");
table.Columns.Add("SenderEmailAddress");
table.Sort("ReceivedTime", Outlook.OlSortOrder.olDescending);
// Add using namespace
// Date received
table.Columns.Add(
"urn:schemas:httpmail:datereceived");
this.dataGridView1.DataSource = table;
// Does not work!!
}
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
When invoking method populateInbox() I still get the error below
Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop. Outlook.Ma ilItem'. This operation failed because the QueryInterface call on the COM component for the interface with IID '{00063034-0000-0000-C000- 0000000000 46}' failed due to the following error: No such interface supported (Exception from HRESULT: 0x80004002 (E_NOINTERFACE)).
Unable to cast COM object of type 'System.__ComObject' to interface type 'Microsoft.Office.Interop.
problem solved, olFolderInbox can contain non mail items, so check the object type before casting.
public void populateInbox()
{
MOI.Outlook.MailItem FItem;
try
{
//check that the item that you are trying to reference as a MailItem is actually a MailItem
FFolder = FNamespace.GetDefaultFolde r(MOI.Outl ook.OlDefa ultFolders .olFolderI nbox);
foreach (Object temp in FFolder.Items)
{
if (temp is MOI.Outlook.MailItem)
{
FItem = (MOI.Outlook.MailItem)temp ;
Mails.Rows.Add(new object[] {
FItem.SenderEmailAddress,
FItem.To,
FItem.CC,
FItem.Subject,
FItem.ReceivedTime,
FItem.Body
});}
}
}
catch (System.Exception e)
{
Debug.WriteLine("Error populateInbox");
throw e;
}
}
public void populateInbox()
{
MOI.Outlook.MailItem FItem;
try
{
//check that the item that you are trying to reference as a MailItem is actually a MailItem
FFolder = FNamespace.GetDefaultFolde
foreach (Object temp in FFolder.Items)
{
if (temp is MOI.Outlook.MailItem)
{
FItem = (MOI.Outlook.MailItem)temp
Mails.Rows.Add(new object[] {
FItem.SenderEmailAddress,
FItem.To,
FItem.CC,
FItem.Subject,
FItem.ReceivedTime,
FItem.Body
});}
}
}
catch (System.Exception e)
{
Debug.WriteLine("Error populateInbox");
throw e;
}
}
Open in new window