{"id":67813,"date":"2006-03-07T11:29:00","date_gmt":"2006-03-07T11:29:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/03\/07\/how-can-i-save-an-excel-spreadsheet-and-then-save-a-copy-as-html\/"},"modified":"2006-03-07T11:29:00","modified_gmt":"2006-03-07T11:29:00","slug":"how-can-i-save-an-excel-spreadsheet-and-then-save-a-copy-as-html","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-save-an-excel-spreadsheet-and-then-save-a-copy-as-html\/","title":{"rendered":"How Can I Save an Excel Spreadsheet, and Then Save a Copy as HTML?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! How can I save an Excel spreadsheet, and then save a copy of that spreadsheet as HTML?<BR><BR>&#8212; TH<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, TH. You know, it\u2019s interesting that you used the word \u201csave\u201d in your question. After all, the Scripting Guy who writes this column (who\u2019s much lazier &#8211; um, much <I>busier<\/I> than usual) just glanced at the clock and realized, \u201cUh-oh: I haven\u2019t written tomorrow\u2019s column yet.\u201d There were only two things that could save him: 1) the end of the world (though no doubt even <I>then<\/I> his editor would want to know where the column was) or, 2) a question that he could answer without having to do any research. TH, you saved the Scripting Guys!<\/P>\n<P>Well, at least <I>this<\/I> Scripting Guy.<\/P>\n<P>In return for your heroic action we\u2019d like to give you something near and dear to our hearts: a script that can save an Excel spreadsheet, and then save a copy of that spreadsheet as HTML. That\u2019s OK; don\u2019t bother to hide your tears. This is a pretty emotional moment for us, too.<\/P>\n<P>Here\u2019s your script:<\/P><PRE class=\"codeSample\">Const xlHTML = 44 <\/p>\n<p>Set objExcel = CreateObject(&#8220;Excel.Application&#8221;)\nobjExcel.Visible = True\nSet objWorkbook = objExcel.Workbooks.Add()\nSet objWorksheet = objWorkbook.Worksheets(1)<\/p>\n<p>objExcel.DisplayAlerts = False<\/p>\n<p>objExcel.Cells(1, 1).Value = &#8220;A&#8221;\nobjExcel.Cells(1, 2).Value = &#8220;B&#8221;\nobjExcel.Cells(1, 3).Value = &#8220;C&#8221;\nobjExcel.Cells(1, 4).Value = &#8220;D&#8221;<\/p>\n<p>objWorkbook.SaveAs &#8220;C:\\Scripts\\Test.xls&#8221;\nobjWorkbook.SaveAs &#8220;C:\\Scripts\\Test.htm&#8221;, xlHTML\n<\/PRE>\n<P>You can see why &#8211; facing a deadline and almost certain doom &#8211; we decided to answer <I>this<\/I> question: there\u2019s hardly anything at all to the script. We start off by defining a constant named xlHTML and setting the value to 44; we\u2019ll use this constant to tell Excel that we want to save a copy of the spreadsheet as HTML. We then have four lines of code that create a visible instance of the <B>Excel.Application<\/B> object and then create a new workbook and a new worksheet for us to play around with.<\/P>\n<P>And then, just for the heck of it, we threw in this line of code:<\/P><PRE class=\"codeSample\">objExcel.DisplayAlerts = False\n<\/PRE>\n<P>Why did we do this? Well, first we\u2019re creating a brand-new worksheet with this script, and then we\u2019re going to save it as C:\\Scripts\\Test.xls (and then as C:\\Scripts\\Test.htm). That\u2019s fine, <I>unless<\/I> a file named Test.xls or Test.htm already exists. In that case Excel won\u2019t automatically overwrite the existing file; instead, it will pop up a dialog box asking if you\u2019re sure that you want to overwrite that file. The dialog box will sit there on screen until you click Yes, No, or Cancel; needless to say, that means your script will also sit there &#8211; doing nothing &#8211; until you click Yes, No, or Cancel.<\/P>\n<P>Unless, that is, you set <B>DisplayAlerts<\/B> to False. When DisplayAlerts is set to False Excel will not display any message boxes; instead, it automatically uses the default option of any message boxes it would otherwise show. For example, the default option for overwriting an existing file is Yes; therefore, instead of displaying a message box and waiting for you to answer, the script simple goes with the default value (yes) and overwrites the file. Make sense?<\/P>\n<P>And, no, we didn\u2019t <I>have<\/I> to use this option here. However, we get a lot of questions about this (\u201cHow can I stop Excel from asking me if I want to overwrite a file?\u201d), and this seemed like a sneaky way to answer two questions for the price of one. Although, now that we think about it, it would have been smarter to save <I>that<\/I> question for tomorrow\u2019s column, putting us ahead of the game for once. Dang.<\/P>\n<P>After configuring the DisplayAlerts property we have some code that simply writes the values A, B, C, and D into four cells of the spreadsheet. That brings us to the last two lines of the script:<\/P><PRE class=\"codeSample\">objWorkbook.SaveAs &#8220;C:\\Scripts\\Test.xls&#8221;\nobjWorkbook.SaveAs &#8220;C:\\Scripts\\Test.htm&#8221;, xlHTML\n<\/PRE>\n<P>In the first line we\u2019re saving our spreadsheet as a spreadsheet; that\u2019s done by calling the <B>SaveAs<\/B> method and including just one parameter, the full path to the new file. In the second line we\u2019re saving a copy of the spreadsheet as an HTML file. Note that we also use the <B>SaveAs<\/B> method and that we again specify the full path to the new file (this time with a .htm file extension). However, we also add a second parameter, the constant xlHTML; as we noted earlier, this tells Excel the file format for the new file. Could we save a file in a format other than HTML? Of course; hey, variety is the spice of life, right? Here are a just few of the constants and their corresponding values that we can use with the SaveAs method:<\/P>\n<TABLE id=\"ENE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Constant<\/B><\/P><\/TD>\n<TD>\n<P class=\"lastInCell\"><B>Value<\/B><\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">xlCSV<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">6<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">xlCSVMac<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">22<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">xlCSVMSDOS<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">24<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">xlCSVWindows<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">23<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">xlDBF4<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">11<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">xlExcel9795<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">43<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">xlHtml<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">44<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">xlSYLK<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">2<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">xlTemplate<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">17<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">xlTextWindows<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">20<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">xlXMLData<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">47<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">xlXMLSpreadsheet<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">46<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>For a complete list, take a peek at the Excel <A href=\"http:\/\/msdn.microsoft.com\/library\/en-us\/vbaxl11\/html\/xlhowConstants_HV01049962.asp\" target=\"_blank\"><B>VBA Language Reference<\/B><\/A> on MSDN. (Look for the <B>xlFileFormat<\/B> enumeration.)<\/P>\n<P>So have the Scripting Guys learned their lesson from this close escape? Will they now do a better job of getting their columns in <I>before<\/I> the deadline? You bet they will; in fact, you can <I>count<\/I> on it.<\/P>\n<P>Just don\u2019t count on it.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I save an Excel spreadsheet, and then save a copy of that spreadsheet as HTML?&#8212; TH Hey, TH. You know, it\u2019s interesting that you used the word \u201csave\u201d in your question. After all, the Scripting Guy who writes this column (who\u2019s much lazier &#8211; um, much busier than usual) just [&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,5],"class_list":["post-67813","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-microsoft-excel","tag-office","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I save an Excel spreadsheet, and then save a copy of that spreadsheet as HTML?&#8212; TH Hey, TH. You know, it\u2019s interesting that you used the word \u201csave\u201d in your question. After all, the Scripting Guy who writes this column (who\u2019s much lazier &#8211; um, much busier than usual) just [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67813","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=67813"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67813\/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=67813"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67813"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67813"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}