{"id":65163,"date":"2007-04-03T20:45:00","date_gmt":"2007-04-03T20:45:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/04\/03\/how-can-i-save-a-table-in-an-access-database-as-a-spreadsheet\/"},"modified":"2007-04-03T20:45:00","modified_gmt":"2007-04-03T20:45:00","slug":"how-can-i-save-a-table-in-an-access-database-as-a-spreadsheet","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-save-a-table-in-an-access-database-as-a-spreadsheet\/","title":{"rendered":"How Can I Save a Table in an Access Database as a Spreadsheet?"},"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 a table in an Access database as an Excel spreadsheet?<BR><BR>&#8212; OT<\/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, OT. You know, we appreciate the question, but where were you last week, when we really needed you? The truth is, last week was excruciatingly dull, at least for two of the Scripting Guys. While Scripting Guy Jean Ross was busy <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/mar07\/hey0330.mspx\"><B>battling ninja warriors<\/B><\/A> in San Diego and Scripting Guy Peter Costantini amused himself by climbing volcanoes in Nicaragua, the remaining Scripting Guys sat around the office all week having conversations like this:<\/P>\n<P>\u201cSure is boring around here, isn\u2019t it?\u201d<\/P>\n<P>\u201cYep.\u201d<\/P>\n<P>\u201cI mean, <I>really<\/I> boring.\u201d<\/P>\n<P>\u201cYep.\u201d<\/P>\n<P>\u201cWould you agree that it\u2019s pretty boring this week?\u201d<\/P>\n<P>\u201cYep.\u201d<\/P>\n<P>In fact, the highlight of the entire week \u2013 at least for Scripting Guy Dean Tsaltas \u2013 occurred when the Microsoft cafeteria served broccoli with cheese sauce <I>two days in a row<\/I>!<\/P>\n<P>We told you that it was a long week.<\/P>\n<P>That\u2019s why we could have used an interesting question like this one <I>last<\/I> week; that would have added a little excitement and pizzazz to our long and lonely days. Now that Jean is back we have all the excitement and pizzazz we could ever ask for; we don\u2019t really need your question anymore. Sorry.<\/P>\n<P>On the other hand, we can\u2019t really say that this week is turning out to be any more exciting than last week did; in addition, Jean doesn\u2019t seem to be anywhere <I>near<\/I> as interesting as we thought she was. So what the heck, OT: let\u2019s see if we can figure out how to save an Access table as an Excel spreadsheet. Just be patient; this could take awhile.<\/P>\n<P>Never mind; turns out that it won\u2019t take very long after all:<\/P><PRE class=\"codeSample\">Const acExport = 1\nConst acSpreadsheetTypeExcel9 = 8<\/p>\n<p>Set objAccess = CreateObject(&#8220;Access.Application&#8221;)\nobjAccess.OpenCurrentDatabase &#8220;C:\\Scripts\\Test.mdb&#8221;<\/p>\n<p>objAccess.DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _\n    &#8220;Employees&#8221;, &#8220;C:\\Scripts\\Employees.xls&#8221;, True\n<\/PRE>\n<P>Yes, we know: it\u2019s great that this task can be accomplished by using just a few lines of code. However, we were hoping to kill a bunch of time by working on this one script; <I>now<\/I> what are we supposed to do?<\/P>\n<P>Explain how the script works? Hmmm, interesting idea; maybe we\u2019ll give that a try.<\/P>\n<P>As you can see, we start out by defining a constant named acExport and setting the value to 1; that tells the script that we want to export data. Does that mean that we could use a similar script to <I>import<\/I> data? As a matter of fact it does, and in <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/apr07\/hey0404.mspx\"><B>tomorrow\u2019s column<\/B><\/A> we\u2019ll show you how to do that very thing. (Now we\u2019re trying to give <I>you<\/I> something to look forward to!) After that we define a second constant \u2013 acSpreadsheetTypeExcel9 \u2013 and set the value to 8; this tells the script what <I>kind<\/I> of spreadsheet we want to create. The constant acSpreadsheetTypeExcel9 creates an Excel 2003 spreadsheet; alternatively, you might want to save your data in a different format. If so then use one of the following constants and their values:<\/P>\n<TABLE class=\"dataTable\" id=\"EVE\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Constant<\/B><\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\"><B>Value<\/B><\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">acSpreadsheetTypeExcel12<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">9<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">acSpreadsheetTypeExcel12Xml<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">10<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">acSpreadsheetTypeExcel3<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">0<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">acSpreadsheetTypeExcel4<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">6<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">acSpreadsheetTypeExcel5<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">5<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">acSpreadsheetTypeExcel7<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">5<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">acSpreadsheetTypeExcel8<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">8<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">acSpreadsheetTypeExcel9<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">8<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">acSpreadsheetTypeLotusWJ2<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">4<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">acSpreadsheetTypeLotusWK1<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">2<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">acSpreadsheetTypeLotusWK3<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">3<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P class=\"lastInCell\">acSpreadsheetTypeLotusWK4<\/P><\/TD>\n<TD class=\"\">\n<P class=\"lastInCell\">7<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>That was all pretty easy, wasn\u2019t it? And, as you\u2019re about to see, it doesn\u2019t get much harder, either. After defining our constants we next use these two lines of code to create an instance of the <B>Access.Application<\/B> object and then open the file C:\\Scripts\\Test.mdb (a feat we accomplish by calling the <B>OpenCurrentDatabase<\/B> method):<\/P><PRE class=\"codeSample\">Set objAccess = CreateObject(&#8220;Access.Application&#8221;)\nobjAccess.OpenCurrentDatabase &#8220;C:\\Scripts\\Test.mdb&#8221;\n<\/PRE>\n<P>At this point we\u2019re just one command away from exporting our data as an Excel spreadsheet. In fact, now that we mention it, we\u2019re just <I>this<\/I> command away from exporting our data as an Excel spreadsheet:<\/P><PRE class=\"codeSample\">objAccess.DoCmd.TransferSpreadsheet acExport, acSpreadsheetTypeExcel9, _\n    &#8220;Employees&#8221;, &#8220;C:\\Scripts\\Employees.xls&#8221;, True\n<\/PRE>\n<P>What we\u2019re doing here is using the <B>DoCmd<\/B> object (a child object of Access.Application) to call the <B>TransferSpreadsheet<\/B> method; this method enables you to export data to or import data from a spreadsheet. As you can see, we simply call the method, passing five parameters:<\/P>\n<TABLE class=\"\" cellSpacing=\"0\" cellPadding=\"0\" border=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>acExport<\/B>, the constant that tells the script that we want to export data rather than import it.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>acSpreadsheetTypeExcel9<\/B>, the constant that specifies the file format for the exported data.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>\u201cEmployees\u201d<\/B>, the name of the table in Test.mdb to be exported.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>\u201cC:\\Scripts\\Employees.xls\u201d<\/B>, the full path to the new Excel file being created.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>True<\/B>, the HasFieldNames parameter, which tells the script to make the field names in the table the first row in the spreadsheet file.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>Believe it or not, that\u2019s all we have to do; if you run this script and then take a look at the folder C:\\Scripts you should see a new spreadsheet file named Employees.xls.<\/P>\n<P>OK, true; it\u2019s not broccoli with cheese sauce. But it\u2019s close.<\/P>\n<P>We hope that helps, OT. And while we appreciate the offer, there\u2019s no need to provide us with another exciting question for tomorrow. After all, tomorrow is Taco Salad Wednesday in the cafeteria, and that alone is <I>more<\/I> than enough excitement for the Scripting Guys.<\/P>\n<DIV><A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/apr07\/hey0403.mspx#top\"><IMG height=\"9\" alt=\"Top of page\" src=\"http:\/\/www.microsoft.com\/library\/gallery\/templates\/MNP2.Common\/images\/arrow_px_up.gif\" width=\"7\" border=\"0\"><\/A><A class=\"topOfPage\" href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/apr07\/hey0403.mspx#top\">Top of page<\/A><\/DIV><A class=\"\" title=\"E2AAC\" name=\"E2AAC\"><\/A>\n<H2>Saving an Access Table as a Spreadsheet Using Windows PowerShell<\/H2>\n<P>Just for the heck of it, we\u2019ve added a new feature to <I>Hey, Scripting Guy!<\/I>: whenever possible, we\u2019ll provide a Windows PowerShell solution as well as our typical VBScript solution. In general we won\u2019t be able to provide much explanation for the PowerShell version, but at least there will be some sample code for you to look at.<\/P>\n<P>You know, sample code like this: <\/P><PRE class=\"codeSample\">$acExport = 1\n$acSpreadsheetTypeExcel9 = 8<\/p>\n<p>$a = New-Object -Comobject Access.Application\n$a.OpenCurrentDatabase(&#8220;C:\\Scripts\\Test.mdb&#8221;)<\/p>\n<p>$a.DoCmd.TransferSpreadsheet($acExport, $acSpreadsheetTypeExcel9, &#8220;Employees&#8221;, `\n    &#8220;C:\\Scripts\\Employees.xls&#8221;, $True)<\/p>\n<p>$a.Quit()<\/PRE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I save a table in an Access database as an Excel spreadsheet?&#8212; OT Hey, OT. You know, we appreciate the question, but where were you last week, when we really needed you? The truth is, last week was excruciatingly dull, at least for two of the Scripting Guys. While Scripting [&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":[710,54,48,49,3,5],"class_list":["post-65163","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-excel-spreadsheet","tag-microsoft-access","tag-microsoft-excel","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I save a table in an Access database as an Excel spreadsheet?&#8212; OT Hey, OT. You know, we appreciate the question, but where were you last week, when we really needed you? The truth is, last week was excruciatingly dull, at least for two of the Scripting Guys. While Scripting [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65163","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=65163"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65163\/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=65163"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65163"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65163"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}