{"id":64783,"date":"2007-05-25T23:19:00","date_gmt":"2007-05-25T23:19:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/05\/25\/how-can-i-export-an-hta-table-to-excel\/"},"modified":"2007-05-25T23:19:00","modified_gmt":"2007-05-25T23:19:00","slug":"how-can-i-export-an-hta-table-to-excel","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-export-an-hta-table-to-excel\/","title":{"rendered":"How Can I Export an HTA Table to Excel?"},"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 put a button in an HTA that, when clicked, exports an HTML table to Excel?<BR><BR>&#8212; DW <\/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, DW. You know, yesterday afternoon the Scripting Guy who writes this column went to get his hair cut, figuring he needs to look his best for <A href=\"http:\/\/www.microsoft.com\/events\/teched2007\/default.mspx\" target=\"_blank\"><B>TechEd 2007<\/B><\/A>. (It was only after he got his hair cut that he realized what a futile gesture that was; he should have stopped by the plastic surgeon\u2019s rather than the barbershop.) As he was parking his car, he noticed the following sign in a restaurant window:<\/P>\n<P>SOUP-FREE<\/P>\n<P>This sign intrigued the Scripting Guy who writes this column to no end. His first thought, of course, was that soup was free at this restaurant: order a bowl of soup and they won\u2019t charge you for it. But then he thought, \u201cMaybe this is like a smoke-free restaurant, except that it\u2019s <I>soup<\/I>-free; maybe you can smoke in there but you can\u2019t order a bowl of soup.\u201d For the Seattle area, that seemed plausible:<\/P>\n<P>\u201cWhere do you want to eat tonight?\u201d<\/P>\n<P>\u201cHow about that new place in Redmond?\u201d<\/P>\n<P>\u201cDo they serve soup there?\u201d<\/P>\n<P>\u201cI think so.\u201d<\/P>\n<P>\u201cThen forget it; I will <I>never<\/I> set foot in any restaurant that serves soup! We either find a soup-free restaurant or we stay home.\u201d<\/P>\n<P>At any rate, if anyone out there is looking for a soup-free restaurant, well, just let us know; we think we have one for you.<\/P>\n<TABLE class=\"dataTable\" id=\"ESD\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD class=\"\">\n<P><B>Note<\/B>. As intriguing as the SOUP-FREE sign is, it still ranks as only the <I>second<\/I>-most intriguing sign the Scripting Guy who writes this column has ever seen. But, then again, it\u2019s going to be hard to top the <A href=\"http:\/\/blogs.msdn.com\/gstemp\/archive\/2004\/02\/13\/72505.aspx\" target=\"_blank\"><B>Certificate of Non-Significance<\/B><\/A>.<\/P>\n<P>And no, the Scripting Guy who writes this column doesn\u2019t actually <I>have<\/I> a Certificate of Non-Significance. Some things are so obvious they don\u2019t need any kind of formal certification.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>If it turns out that you are violently opposed to the whole idea of soup, well, we have good news for you: today\u2019s solution is also 100% soup-free. The truth is, we didn\u2019t know of any simple and straightforward way to export an HTML table to Excel (with or without soup). However, we came up with something we thought might be even better: a simple and straightforward way to copy an HTML table and then paste it into Excel. In fact, we came up with <I>this<\/I> way to copy an HTML table and then paste it into Excel:<\/P><PRE class=\"codeSample\">&lt;Script Language=&#8221;VBScript&#8221;&gt;\n    Sub RunScript\n        strCopy = MyTable.InnerHTML\n        document.parentwindow.clipboardData.SetData &#8220;text&#8221;, strCopy<\/p>\n<p>        Set objExcel = CreateObject(&#8220;Excel.Application&#8221;)\n        objExcel.Visible = True<\/p>\n<p>        Set objWorkbook = objExcel.Workbooks.Add()\n        Set objWorksheet = objWorkbook.Worksheets(1)\n        objWorksheet.Paste\n    End Sub\n&lt;\/Script&gt;<\/p>\n<p>&lt;body&gt;\n    &lt;span id=MyTable&gt;\n        &lt;table border=&#8221;1&#8243; width=&#8221;100%&#8221; id=&#8221;table1&#8243;&gt;\n    &lt;tr&gt;\n&lt;td&gt;Cell 1&lt;\/td&gt;\n&lt;td&gt;Cell 2&lt;\/td&gt;\n&lt;td&gt;Cell 3&lt;\/td&gt;\n    &lt;\/tr&gt;\n    &lt;tr&gt;\n&lt;td&gt;Cell 4&lt;\/td&gt;\n&lt;td&gt;Cell 5&lt;\/td&gt;\n&lt;td&gt;Cell 6&lt;\/td&gt;\n    &lt;\/tr&gt;\n    &lt;tr&gt;\n&lt;td&gt;Cell 7&lt;\/td&gt;\n&lt;td&gt;Cell 8&lt;\/td&gt;\n&lt;td&gt;Cell 9&lt;\/td&gt;\n    &lt;\/tr&gt;\n        &lt;\/table&gt;\n    &lt;\/span&gt;\n    &lt;br&gt;\n    &lt;input id=runbutton type=&#8221;button&#8221; value=&#8221;Run Button&#8221; onClick=&#8221;RunScript&#8221;&gt;\n&lt;\/body&gt;\n<\/PRE>\n<P>What we have here is a very simple little HTA, one that looks a lot like this:<\/P><IMG height=\"220\" alt=\"HTA\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/htatable.jpg\" width=\"293\" border=\"0\"> \n<P><BR>As you can see, the bulk of our little HTA is a table, in this case a nine-cell table (three rows by three columns). Per DW\u2019s email, we\u2019ve put this table inside a &lt;SPAN&gt; tag with the <B>ID<\/B> MyTable; here\u2019s the code that creates the &lt;SPAN&gt; and initializes the table:<\/P><PRE class=\"codeSample\">&lt;span id=MyTable&gt;\n    &lt;table border=&#8221;1&#8243; width=&#8221;100%&#8221; id=&#8221;table1&#8243;&gt;\n<\/PRE>\n<P>For those of you <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/htas.mspx\"><B>new to HTML tagging<\/B><\/A>, a &lt;SPAN&gt; is simply a named area of your document; we can define an area of the document and then stuff that area full of anything we want (in this case, the only thing we\u2019ve put in the &lt;SPAN&gt; is a table.) The cool part, however, is this: as long as we give the &lt;SPAN&gt; an ID we can then manipulate that portion of the document programmatically. That might mean making dynamic changes to that section of the HTA. Or, as is the case here, it might be doing nothing more than grabbing all the information inside the &lt;SPAN&gt;.<\/P>\n<P>The long and short of it? We have a table in our HTA <I>and<\/I> we have a mechanism for identifying and manipulating that table.<\/P>\n<P>We only have one other element in the HTA body: a button (labeled <B>Run Button<\/B>) that, when clicked, runs a subroutine named RunScript:<\/P><PRE class=\"codeSample\">&lt;input id=runbutton type=&#8221;button&#8221; value=&#8221;Run Button&#8221; onClick=&#8221;RunScript&#8221;&gt;\n<\/PRE>\n<P>As you might expect, RunScript is the subroutine that\u2019s going to grab a copy of the table and somehow get that copy into an Excel spreadsheet.<\/P>\n<P>Good question: how <I>does<\/I> this subroutine grab a copy of the table and get that copy into an Excel spreadsheet? Let\u2019s see if we can figure that out. <\/P>\n<P>To begin with, we use this line of code to connect to the MyTable &lt;SPAN&gt; and assign the value of the <B>InnerHTML<\/B> property to a variable named strCopy:<\/P><PRE class=\"codeSample\">strCopy = MyTable.InnerHTML\n<\/PRE>\n<P>What\u2019s the InnerHTML property? That\u2019s basically everything that falls between the beginning &lt;SPAN&gt; tag and the ending &lt;\/SPAN&gt; tag; that includes both the data found in the table and the HTML tagging for creating and formatting that table. In other words, we now have all the information necessary for re-creating the table stashed away in the variable strCopy. Once that\u2019s done we use this line of code to copy that information to the Windows Clipboard:<\/P><PRE class=\"codeSample\">document.parentwindow.clipboardData.SetData &#8220;text&#8221;, strCopy\n<\/PRE>\n<P>What we\u2019re doing here is calling the <B>SetData<\/B> method, a method that just happens to belong to the <B>clipboardData<\/B> object. In addition, we\u2019re passing SetData two 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>&#8220;text&#8221;<\/B>, which represents the type of data being copied to the Clipboard.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>strCopy<\/B>, the actual data we want to copy to the Clipboard.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>As you probably guessed, after calling SetData our table will be copied to the Windows Clipboard; in turn, that means we\u2019re ready to paste this data into Excel. To that end, we first create an instance of the <B>Excel.Application<\/B> object and then set the <B>Visible<\/B> property to True:<\/P><PRE class=\"codeSample\">Set objExcel = CreateObject(&#8220;Excel.Application&#8221;)\nobjExcel.Visible = True\n<\/PRE>\n<P>That gives us a running instance of Excel that we can view onscreen. We then take that running instance, create a new workbook, and bind to the first worksheet in that workbook:<\/P><PRE class=\"codeSample\">Set objWorkbook = objExcel.Workbooks.Add()\nSet objWorksheet = objWorkbook.Worksheets(1)\n<\/PRE>\n<P>So then how do we paste in our table? Why, by calling the <B>Paste<\/B> method, of course:<\/P><PRE class=\"codeSample\">objWorksheet.Paste\n<\/PRE>\n<P>Yes, just like a soup-free restaurant, this sounds too good to be true. But give it a try and see for yourself:<\/P><IMG height=\"285\" alt=\"Microsoft Excel\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/exceltable.jpg\" width=\"295\" border=\"0\"> \n<P><BR>Truth be told, this is pretty darn easy; play around with this a little bit and you\u2019ll quickly catch on. And you might have to play around with it by yourself because, for better or worse, we have to call it a day; after all, we need to create a new sign for the <I>Hey, Scripting Guy!<\/I> column:<\/P>\n<P>SCRIPT-FREE<\/P>\n<P>So does that mean that all the scripts in <I>Hey, Scripting Guy!<\/I> are free of charge \u2026 or does that mean that <I>Hey, Scripting Guy!<\/I> doesn\u2019t even <I>try<\/I> to include script code any more? Good question. (Although it is getting harder and harder for us to find room for script code, what with the need to talk about haircuts, soup-free restaurants, and Certificates of Non-Significance.)<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I put a button in an HTA that, when clicked, exports an HTML table to Excel?&#8212; DW Hey, DW. You know, yesterday afternoon the Scripting Guy who writes this column went to get his hair cut, figuring he needs to look his best for TechEd 2007. (It was only after [&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":[48,49,3,4,5,30],"class_list":["post-64783","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-excel","tag-office","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-web-pages-and-htas"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I put a button in an HTA that, when clicked, exports an HTML table to Excel?&#8212; DW Hey, DW. You know, yesterday afternoon the Scripting Guy who writes this column went to get his hair cut, figuring he needs to look his best for TechEd 2007. (It was only after [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64783","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=64783"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64783\/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=64783"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64783"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64783"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}