Welcome to MSDN Blogs Sign in | Join | Help

Contacts to Outlook

As you can probably tell, it's a Friday afternoon, and I still have a load more of this to get through before the weekend.  On Tuesday, I'm presenting this story at PDC.  But the story is hidden behind all the little bits of code right now.  So let's cut the code, make room for more of the story soon.

We scanned Groove workspaces and tools, and loaded Groove Forms data into a DataGridView for display in a form.  The final piece to that application is to drop the selected rows of data into Outlook's "Contacts".

I implemented this by an accessor method in my form class:

// Access to the currently-selected-contacts list

public ArrayList GetSelectedData()

{

// read the VCard for each selected contact,

// and add it to the VCardList.

ArrayList dataRows = new ArrayList();

foreach (DataGridViewRow dgvr in dataGridView1.SelectedRows)

{

dataRows.Add(((System.Data.DataRowView)dgvr.DataBoundItem).Row);

}

// Equivalent for DataView:

//int n = 0;

//foreach (System.Data.DataRow row in ((System.Data.DataTable)dataGridView1.DataSource).Rows)

//{

// if (dataView.IsSelected(n))

// {

// dataRows.Add(row);

// }

// n++;

//}

return dataRows;

}

And when the form is closed, the caller (my addin class) puts the selected rows into Outlook like this:

private void OnToolbarButtonClick(CommandBarButton cmdBarbutton,ref bool cancel)

{

try

{

// Display the contact picker form

Form1 selectionForm = new Form1();

if( selectionForm.ShowDialog() == System.Windows.Forms.DialogResult.OK )

{

ArrayList rowList = selectionForm.GetSelectedData();

foreach (System.Data.DataRow row in rowList)

{

Microsoft.Office.Interop.Outlook.ContactItem contactItem;

contactItem = (Microsoft.Office.Interop.Outlook.ContactItem)applicationObject.CreateItem(Microsoft.Office.Interop.Outlook.OlItemType.olContactItem);

if (row.Table.Columns["FullName"] != null) contactItem.FullName = (string)row["FullName"];

if (row.Table.Columns["NickName"] != null) contactItem.NickName = (string)row["NickName"];

if (row.Table.Columns["FirstName"] != null) contactItem.FirstName = (string)row["FirstName"];

if (row.Table.Columns["LastName"] != null) contactItem.LastName = (string)row["LastName"];

if (row.Table.Columns["Suffix"] != null) contactItem.Suffix = (string)row["Suffix"];

if (row.Table.Columns["JobTitle"] != null) contactItem.JobTitle = (string)row["JobTitle"];

if (row.Table.Columns["CompanyName"] != null) contactItem.CompanyName = (string)row["CompanyName"];

// I think that's enough to start with. Now save it to Outlook.

contactItem.Close(Microsoft.Office.Interop.Outlook.OlInspectorClose.olSave);

}

}

}

catch( Exception e ) { /* stuff */ }

}

So it's only at this last stage that we need any knowledge of the fields in our data.

Anyone reading this: now would be a great time to ask questions ("hughpyle" at...)!

Published Friday, September 09, 2005 2:49 PM by hpyle
Filed under:

Comments

No Comments

Anonymous comments are disabled
 
Page view tracker