{"id":64383,"date":"2007-07-25T01:00:00","date_gmt":"2007-07-25T01:00:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/07\/25\/how-can-i-save-all-my-contacts-as-vcards\/"},"modified":"2007-07-25T01:00:00","modified_gmt":"2007-07-25T01:00:00","slug":"how-can-i-save-all-my-contacts-as-vcards","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-save-all-my-contacts-as-vcards\/","title":{"rendered":"How Can I Save All My Contacts as VCards?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"> \n<P>Hey, Scripting Guy! How can I save all my contacts as VCards?<BR><BR>&#8212; ET <\/P><IMG height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\"><\/A> \n<P>Hey, ET. You know, we have to tell you the truth: after nearly 800 <I>Hey, Scripting Guy!<\/I> articles the Scripting Guy who writes this column is beginning to get a little burnt-out. Fortunately, the Scripting Editor has often volunteered to do the column for him, and the Scripting Guy who writes this column has decided to take her up on that generous offer. Therefore, and without any further adieu, let\u2019s turn today\u2019s <I>Hey, Scripting Guy!<\/I> over to the Scripting Editor:<\/P>\n<TABLE class=\"dataTable\" id=\"ECD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P>Hey, ET. Oh, ET: I get it; that was, like, the name of that Martian guy, right? That\u2019s, like, really funny, you know, I mean, I can\u2019t, like, wait to tell Cassandra.<\/P>\n<P>Um, so, anyway, ET (tee-hee!), you know, it sounds like you want to save your contacts as \u2026something \u2026 oh, wait, I know: you want to save your contacts as, like, VCards, right?. So, like, do you have any idea what VCards <I>are<\/I>, you know, I mean, are they, like, <I>better<\/I> than contacts, so, like, maybe that\u2019s why you want to save your contacts as those card thingies? I mean, because I really <I>like<\/I> contacts, you know, like, my friend, Jessica, she has these contacts that can, like, make her eyes look like different colors, you know, like, on Monday, you know, maybe she has, like blue eyes, and then, like, on Tuesday or whatever she changes contacts and, like, suddenly it looks like she has <I>green <\/I>eyes, and, like, that is <I>way<\/I> cool, you know, and like Jessica\u2019s boyfriend, Todd, Todd\u2019s like, \u201cWow, it\u2019s like I have a different girlfriend every day of the week!\u201d and I\u2019m thinking, I wish you <I>did<\/I> have a different girlfriend every day of the week, because Todd is a real hunk and I would <I>totally<\/I> go out with him except, like, Jessica\u2019s my friend, you know, so I guess that wouldn\u2019t be very cool, you know? So anyway, contacts are awesome but, like, maybe these VCards, whatever they are, well, like, maybe they\u2019re better, you know?<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>OK, thank you, Scripting Editor. That helped; the Scripting Guy who writes this column suddenly feels much better and much more refreshed now. In fact, we\u2019ll tell you what: why don\u2019t you finish doing whatever Scripting Editors do, and we\u2019ll show ET how to \u2013 um, yes, we get it, although we don\u2019t believe the real ET was actually a <I>Martian<\/I>. Anyway, why don\u2019t you go do whatever it is you do and we\u2019ll show ET a script that can save all his contacts as VCards:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>Const olFolderContacts = 10\nConst olVCard = 6<\/p>\n<p>Set objOutlook = CreateObject(&#8220;Outlook.Application&#8221;)\nSet objNamespace = objOutlook.GetNamespace(&#8220;MAPI&#8221;)<\/p>\n<p>Set colContacts = objNamespace.GetDefaultFolder(olFolderContacts).Items<\/p>\n<p>For Each objContact In colContacts\n    strName = objContact.FirstName &amp; objContact.LastName\n    strPath = &#8220;C:\\Test\\&#8221; &amp; strName &amp; &#8220;.vcf&#8221;\n    objContact.SaveAs strpath, olVCard\nNext\n<\/PRE>\n<P>As you can see, ET, the script \u2013 sorry; just a second. Yes, those shoes <I>are<\/I> darling, just darling. And they look totally awesome with that sweater. <\/P>\n<P>Totally.<\/P>\n<P>Anyway, as we were saying, the script starts out by implementing the <B>On Error Resume Next<\/B> statement. Whenever we work with contacts we\u2019re prone to getting somewhat-inexplicable errors from time-to-time, errors that, to be honest, don\u2019t make a lot of sense to us; for example, the script might fail, complaining that a valid contact property (like FirstName) isn\u2019t a valid property after all. We\u2019re not sure if this is a generic problem or if it just has something to do with our copy of Outlook; however, tossing in the On Error Resume Next statement seems to take care of the problem, so we did that here just as a precaution.<\/P>\n<P>Our first <I>real<\/I> bit of coding is to define a pair of constants: olFolderContacts, which tells the script which Outlook folder we want to work with; and olVCard, which tells the script the file format to use when saving the contact information. After defining the constants we create an instance of the <B>Outlook.Application<\/B> object, then use the following line of code to bind to the MAPI namespace (a required step, even though the MAPI namespace is the <I>only<\/I> namespace we can bind to):<\/P><PRE class=\"codeSample\">Set objNamespace = objOutlook.GetNamespace(&#8220;MAPI&#8221;)\n<\/PRE>\n<P>Once we\u2019ve successfully connected to Outlook we can then use the following line of code and the <B>GetDefaultFolder<\/B> method to retrieve a collection of all the items found in the Contacts folder:<\/P><PRE class=\"codeSample\">Set colContacts = objNamespace.GetDefaultFolder(olFolderContacts).Items\n<\/PRE>\n<P>That was, like, way easy, you know?<\/P>\n<P>Sorry; we couldn\u2019t resist.<\/P>\n<P>Actually, that <I>was<\/I> way easy and, best of all, the rest of the script is just as easy. Our next chore is to set up a For Each loop to loop through the collection of contacts. As everyone knows \u2013 well, OK, as <I>most<\/I> people know \u2013 VCard is just another file format; consequently, that means that we can\u2019t save contact information as a VCard unless we specify the complete file path for this new file. We decided to create a file name based on the contact\u2019s first name and last name; thus we use this line of code to assign the values of the <B>FirstName<\/B> and <B>LastName<\/B> properties to a variable christened strName:<\/P><PRE class=\"codeSample\">strName = objContact.FirstName &amp; objContact.LastName\n<\/PRE>\n<P>If we have a contact named Ken Myer that\u2019s going to result in a file name that looks like this:<\/P><PRE class=\"codeSample\">KenMyer\n<\/PRE>\n<P>Once we have a unique file name we can then construct the complete path:<\/P><PRE class=\"codeSample\">strPath = &#8220;C:\\Test\\&#8221; &amp; strName &amp; &#8220;.vcf&#8221;\n<\/PRE>\n<P>Nothing too fancy here: we\u2019re just combing the folder path <I>C:\\Test\\<\/I> with the value of the variable strName and <I>.vcf<\/I>, which represents the file extension (because VCards use a .VCF file extension). From there we go ahead and create a VCard for our first contact, something we do by calling the <B>SaveAs<\/B> method, passing the file path (the variable strPath) and the constant olVCard as the two method parameters:<\/P><PRE class=\"codeSample\">objContact.SaveAs strpath, olVCard\n<\/PRE>\n<TABLE class=\"dataTable\" id=\"EFG\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Important<\/B>. Because creating a VCard requires you to access the contact\u2019s email address this script won\u2019t just run the moment you kick it off; instead, Outlook is going to pop up a message informing you that the script is trying to access address data and ask if you want to allow this access. You must answer \u201cYes\u201d (or \u201cAllow\u201d) in order for the script to run. And, sadly, there\u2019s no way \u2013 short of reconfiguring your Exchange setup \u2013 to bypass or automate the process of answering this message box. Just something to keep in mind.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>After that we loop around and repeat the process with the next contact in the collection. When all is said and done the folder C:\\Test should include a bunch of files similar to these:<\/P><PRE class=\"codeSample\">KenMyer.vcf\nJonathanHaas.vcf\nPilarAckerman.vcf\n<\/PRE>\n<P>That should do it, ET. And now we\u2019ll let the Scripting Editor have the final word on today\u2019s column:<\/P>\n<TABLE class=\"dataTable\" id=\"ETG\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">Um, like, you know?<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Thank you, Scripting Editor; we couldn\u2019t have said it better ourselves.<\/P>\n<TABLE class=\"dataTable\" id=\"E3G\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Ed<\/B><B>itor\u2019s Note: <\/B>Given that the Scripting Editor didn\u2019t sound like a ditzy teenager even when she <I>was<\/I> a teenager (which really wasn\u2019t that long ago, by the way), we\u2019re not sure where the Scripting Guy who writes this column got the preceding text. What we <I>are<\/I> sure of is that the Scripting Guy who writes this column will be apologizing profusely for painting the Scripting Editor in such a poor light, just as soon as he\u2019s done washing her car\u2026and cleaning her office\u2026and walking her dog\u2026and bringing her donuts for breakfast every morning for the rest of her life\u2026.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I save all my contacts as VCards?&#8212; ET Hey, ET. You know, we have to tell you the truth: after nearly 800 Hey, Scripting Guy! articles the Scripting Guy who writes this column is beginning to get a little burnt-out. Fortunately, the Scripting Editor has often volunteered to do the [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[212,49,3,5],"class_list":["post-64383","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-outlook","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I save all my contacts as VCards?&#8212; ET Hey, ET. You know, we have to tell you the truth: after nearly 800 Hey, Scripting Guy! articles the Scripting Guy who writes this column is beginning to get a little burnt-out. Fortunately, the Scripting Editor has often volunteered to do the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64383","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=64383"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64383\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=64383"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64383"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64383"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}