csv file

Exporting Contacts & Subfolders to a CSV File in Outlook 2007

Recently I needed to export some contacts from Outlook 2007.  A lot of contacts (200+).  All of which were neatly nestled in subfolders inside of subfolders.  I figured this would be a quick task, as I have exported many things from Outlook without issue.  I quickly found this not to be the case.

When you export mail and other items in Outlook, you get the option to choose which subfolders to include.  Outlook then packages this all nice and neat in the format you selected.  Contacts are a different game, it seems.  Contacts can be exported to .pst and will retain their folder structure.  But exporting to a file requires that all contacts be in the root.  My structure included MANY subfolders so this was not much help.

"To work around this issue, copy or move the addresses from the subfolders to the main identity's Contacts folder within the Windows Address Book. You can then export addresses from subfolders to another address book or program. "

That article was for Outlook Express 5; however, reading several message boards and posts revealed the same issue existed in Outlook 2007.  And Microsoft’s solution would not do.  

So I wrote a script that would access my Outlook, read through my contacts and their subfolders, and write out each entry.  It's a pretty simple application and was only for this purpose. 


1. Create a simple winform to display my contacts:

 

 

2.  Use the Outlook Interops to access my mail:


Microsoft.Office.Interop.Outlook.Application oApp = new
Microsoft.Office.Interop.Outlook.Application();Microsoft.Office.Interop.Outlook.

NameSpace oNS = oApp.GetNamespace("mapi");
 
 

3.  Loop through each folder / subfolder to get down to my Contacts folder (“Sample”):


foreach (Microsoft.Office.Interop.Outlook.Folder fldr in oNS.Folders)

 

{

if (fldr.Name.ToString() == "Mailbox - SOLTIS, Bryan")

{

foreach (Microsoft.Office.Interop.Outlook.Folder fldr2 in fldr.Folders)

{

if (fldr2.Name.ToString() == "Sample")

{
 

4.  Loop through my “Sample” contacts folder and find all the contacts.  Go down a few levels to get subfolders in folders.  Write each contact out to a textbox.  Append the folder name and appropriate information for each:

foreach (Microsoft.Office.Interop.Outlook.Folder fldr3 in fldr2.Folders)

 

{

foreach (Microsoft.Office.Interop.Outlook.Folder fldr4 in fldr3.Folders)

{

if (fldr4.Items.Count > 0)

{

i = 1;

while (i <= fldr4.Items.Count)

{

Microsoft.Office.Interop.Outlook.ContactItem ci1 = (Microsoft.Office.Interop.Outlook.ContactItem )fldr4.Items[i];

textBox1.Text += "\"" + fldr3.Name + "\",\"" + fldr4.Name + "\",\"" + ci1.FirstName +  "\",\"" +  ci1.LastName + "\",\"" + ci1.Email1Address + "\"\r\n";

i += 1;

}

}

foreach (Microsoft.Office.Interop.Outlook.Folder fldr5 in fldr4.Folders)

{

if (fldr5.Items.Count > 0)

{

i = 1;

while (i <= fldr5.Items.Count)

{

Microsoft.Office.Interop.Outlook.ContactItem ci1 = (Microsoft.Office.Interop.Outlook.ContactItem)fldr5.Items[i];

textBox1.Text += "\"" + fldr3.Name + "\",\"" + fldr4.Name + "/" + fldr5.Name + "\",\"" + ci1.FirstName + "\",\"" + ci1.LastName + "\",\"" + ci1.Email1Address + "\"\r\n";

i += 1;

}

}

}

}

} 

 

Author

Wiz E. Wig, Mascot & Director of Magic
Wiz E. Wig

Director of Magic

Read more