Bryan Soltis

Senior Software Engineer
MCPD: Enterprise Application Developer 3.5
MCSD.NET, MCAD, MCP

Exporting Contacts and subfolders to a CSV file in Outlook 2007

Friday, December 26, 2008 05:05 | posted by: bryan

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.

I researched this issue and found this KB article from Microsoft: http://support.microsoft.com/kb/241875

"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;

}

}

}

}

} 


Attached is the full script (link below
).  It’s pretty basic and contains no error handler, etc.  Some notes about the script:

- Substitute “Sample” for your Contacts folder name
- The script assumes you have no contacts in the root of your contacts. 
- The script will currently step into your contacts subfolders 3 deep. 

Source: Form1.cs (3.39 kb)

 

Tags:

PermaLink   E-Mail Article   RSS Comment Feed  

Comments

January 12. 2009 21:03

Carlos

Hi,

Sorry to make a typical newbie question but I'm trying to solve the same problem and I Can't figure out the way to execute this script in my computer. Is there an easy way to do it?

Thanks,

Carlos

Carlos Spain

January 12. 2009 21:09

bryan

Carlos,

I had written the code in Visual Sutido 2008 and ran it locally form my machine (as part of a Visual Studio project).  You would need to have that installed, then create a simple form and incorporate the code sample, modifying it apporpiately for your file structure.

I could provide the actual project that contains the form, but it was hard-coded for my file structure and would not work unless you modified the .cs file.

-Bryan

bryan United States

February 10. 2009 16:41

Buzz Zhang

Here is another solution if you feel interest:

http://etienne.giraudy.net/blog/?p=52

Buzz Zhang People's Republic of China

July 20. 2009 15:03

Addicting Games

This is what I'm looking for... My friend is very stressed because she don't know how to move her contacts and subfolders from her old office computer into her outlook in her house,,,

Addicting Games

September 23. 2009 16:57

Adam

Nice article.

The code works fine and is a good starting point for me making a custom module. Thanks for the module...

Adam

October 20. 2009 07:14

Play Online

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

Play Online

May 9. 2010 18:48

A dude

It's silly to ask somebody to dump hundreds of sub folders into the main contact folder.  A software solution or scripted solution is the only way.

A dude

Add comment


(Will show your Gravatar icon)
Click to change captcha
biuquote
  • Comment
  • Preview
Loading