Does any body know how to save your contacts in outlook to the harddrive. i no how to list them. but then i tried the save as methosd.

oContact.SaveAs("C:\contacts\" + list.selectedItem + ".vcf", null);


and that doesnt work. Any ideas?


my code...

Outlook._Application olApp = new Outlook.ApplicationClass();
            Outlook._NameSpace olNs = olApp.GetNamespace("MAPI");
            Outlook.MAPIFolder oContacts = olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
            Outlook.Items oItems = oContacts.Items;
            for (int i = 1; i <= oItems.Count; i++)
            {
                Outlook._ContactItem oContact = (Outlook._ContactItem)oItems.Item(i);
                list.Items.Add(oContact.FullName);
            
             
                oContact.SaveAs("C:\\apps\\" + list.SelectedItem,null);//error here
                oContact = null;
            }

EDIT:


here is the error


Can't write to file: C:\Apps\. Right-click the folder that contains the file you want to write to, and then click Properties on the shortcut menu to check your permissions for the folder.

yea i have permisstion i am admin and all....it gives that eroor no matter where i write

Recommended Answers

All 9 Replies

Hello, you should check out the value of list.SelectedItem in the debugger (yes, that simple :D), and see what it contains (i think i already told you that way to proceed in another thread ;))

are you trying to create a file that outlook can read? or is this copying the contacts for a different use?

well if you havent figured it out yet its because you are trying to use a selection in list before a selection can even be made.
in order to use the selection you need to click on it.

i fixed your code to make iit work, which adds the contact to the listbox and saves it using the current contact name, which is what the code should have done.

although. if there was a selection in the list i think it would recreate the same file during each loop, which could be a nother cause for a error, because the selection wont change just because you are adding and item

oh one more thing, dont expect to learn how things work if you just copy and paste off websites, and modifying it slightly. i googled this and found it on m$'s site lol

Outlook._Application olApp = new Outlook.ApplicationClass();
			Outlook._NameSpace olNs = olApp.GetNamespace("MAPI");
			Outlook.MAPIFolder oContacts = olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
			Outlook.Items oItems = oContacts.Items;
			for (int i = 1; i <= oItems.Count; i++)
			{
				Outlook._ContactItem oContact = (Outlook._ContactItem)oItems.Item(i);
				list.Items.Add(oContact.FullName);

            
             
				oContact.SaveAs(@"C:\" + oContact.FullName, Type.Missing);
				oContact = null;

are you trying to create a file that outlook can read? or is this copying the contacts for a different use?

Just copy the contacts for a different use. Like save on HD. But they are listed in a listbox, as you can see by the code. So i want it to save all the ones in the listbox. Any ideas on how to do this?

-T

i dont know why i am giving you such simple code but here

for(int i = 0; i <= listBox1.Items.Count -1; i++)
			{
				SaveThis = listBox1.Items[i];
			}

it could also use foreach but i wasnt sure what type each item was(maybe string or item something)

"SaveThis" could be either a method to append a file or and object that stores the data to be saved

well if you havent figured it out yet its because you are trying to use a selection in list before a selection can even be made.
in order to use the selection you need to click on it.

i fixed your code to make iit work, which adds the contact to the listbox and saves it using the current contact name, which is what the code should have done.

although. if there was a selection in the list i think it would recreate the same file during each loop, which could be a nother cause for a error, because the selection wont change just because you are adding and item

oh one more thing, dont expect to learn how things work if you just copy and paste off websites, and modifying it slightly. i googled this and found it on m$'s site lol

Outlook._Application olApp = new Outlook.ApplicationClass();
			Outlook._NameSpace olNs = olApp.GetNamespace("MAPI");
			Outlook.MAPIFolder oContacts = olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
			Outlook.Items oItems = oContacts.Items;
			for (int i = 1; i <= oItems.Count; i++)
			{
				Outlook._ContactItem oContact = (Outlook._ContactItem)oItems.Item(i);
				list.Items.Add(oContact.FullName);

            
             
				oContact.SaveAs(@"C:\" + oContact.FullName, Type.Missing);
				oContact = null;

yea, but i didnt get if from MS. i got a a little help and modified it greatly ( i didnt post ALL of it thats just the listing part...). I had aleady tried somthing silmiliar to you SaveAs Method And it didnt work. And that didnt work either. And i do learn from it. I guess we all learn differently. ;)

Thanks.

-T

if my code didnt work, then you prly have access errors, because this worded perfectly for me.

oh well that code was originally from ms site. anyways what formay are you saving it in?

yea, but i didnt get if from MS. i got a a little help and modified it greatly ( i didnt post ALL of it thats just the listing part...). I had aleady tried somthing silmiliar to you SaveAs Method And it didnt work. And that didnt work either. And i do learn from it. I guess we all learn differently. ;)

Thanks.

-T

I just directly used your code

Outlook._Application olApp = new Outlook.ApplicationClass();
			Outlook._NameSpace olNs = olApp.GetNamespace("MAPI");
			Outlook.MAPIFolder oContacts = olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
			Outlook.Items oItems = oContacts.Items;
			for (int i = 1; i <= oItems.Count; i++)
			{
				Outlook._ContactItem oContact = (Outlook._ContactItem)oItems.Item(i);
				list.Items.Add(oContact.FullName);

            
             
				oContact.SaveAs(@"C:\" + oContact.FullName, Type.Missing);
				oContact = null;

it it says operation failed. I use this code on load method. I wasnt aware that you could specifya format. i must have overlooked that :rolleyes:

Thanks.

T

Ok, so i figured this out quite a while ago....But forgot to post solution. Well here it is.

contactlist.Columns.Add("Name", 100, HorizontalAlignment.Left);
            contactlist.Columns.Add("Job Title", 100, HorizontalAlignment.Left);
            contactlist.Columns.Add("Company", 100, HorizontalAlignment.Left);
            contactlist.Columns.Add("Email", 100, HorizontalAlignment.Left);

            Outlook._Application olApp = new Outlook.ApplicationClass();
            Outlook._NameSpace olNs = olApp.GetNamespace("MAPI");
            Outlook.MAPIFolder oContacts = olNs.GetDefaultFolder(Outlook.OlDefaultFolders.olFolderContacts);
            Outlook.Items oItems = oContacts.Items;
            for (int i = 1; i <= oItems.Count; i++)
            {
                Outlook._ContactItem oContact = (Outlook._ContactItem)oItems.Item(i);
                ListViewItem itm = new ListViewItem();
                itm.Text = oContact.FullName;
                itm.SubItems.Add(oContact.JobTitle);
                itm.SubItems.Add(oContact.CompanyName);
                itm.SubItems.Add(oContact.Email1Address);
                contactlist.Items.Add(itm);
                oContact.SaveAs("C:\\Contacts\\" + oContact.FullName + ".vcf", Outlook.OlSaveAsType.olVCard);
                oContact = null;
            }
        }

Thats lists all of out look contacts in a listview. Then it copies them to the C Drive. Note if you dont want it to save them leave out this

oContact.SaveAs("C:\\Contacts\\" + oContact.FullName + ".vcf", Outlook.OlSaveAsType.olVCard);

Well there it is :)

-T

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.