{"id":67443,"date":"2006-04-27T11:56:00","date_gmt":"2006-04-27T11:56:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/04\/27\/how-can-i-print-just-the-text-area-of-an-hta\/"},"modified":"2006-04-27T11:56:00","modified_gmt":"2006-04-27T11:56:00","slug":"how-can-i-print-just-the-text-area-of-an-hta","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-print-just-the-text-area-of-an-hta\/","title":{"rendered":"How Can I Print Just the Text Area of an HTA?"},"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! I have an HTA where information gets written to a text area. How can I print just the information found in that text area?<BR><BR>&#8212; DS<\/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, DS. We want you to level with us here: are the Scripting Guys dead? We ask that because we saw the movie <I>The Sixth Sense<\/I> a few years ago and we understand how this life\/death thing works. If you\u2019re familiar with <I>The Sixth Sense<\/I>, you\u2019ll recall that there were all these eerie happenings and coincidences which could only be explained by one thing: the protagonist (Bruce Willis) was dead. That made for a nifty little movie, only now we Scripting Guys seem to be involved with a bunch of eerie happenings ourselves; after all, in the span of three days we received two emails from people asking these very same question: how can I print just the text area of an HTML Application (HTA)? Coincidence? Let\u2019s hope so; otherwise there can be only one explanation: the Scripting Guys are dead.<\/P>\n<TABLE id=\"E6C\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Hey, don\u2019t worry about us. After all, one of the nice things about being a Scripting Guy is that it doesn\u2019t matter much whether we\u2019re dead or alive: our days go pretty much the same either way.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Of course, the premise of <I>The Sixth Sense<\/I> is that even though Bruce Willis was dead he refused to acknowledge it. Taking a page from Bruce\u2019s book, we\u2019ve decided to go ahead and answer your question as if we were are still alive, too. And we <I>were<\/I> able to come up with an answer for you, even if it was somewhat less-than-elegant. For some reason we had the notion that there was a very straightforward way to print just the information found in a text area, but when we went looking for that answer we couldn\u2019t find it. After a brief but fruitless search we gave up and came up with a workaround that accomplishes the same thing. (And, yes, had we been alive we might have had the energy to search a little harder. But we suppose you have to expect <I>some<\/I> drawbacks to being dead.)<\/P>\n<P>At any rate, here\u2019s a very simple HTA that contains just two items: a text area named DataArea and a button labeled <B>Print<\/B>. Any time you click the <B>Print<\/B> button the HTA saves the information in the text area to a temporary file and then uses the <B>Shell.Application<\/B> object to print that file. Here\u2019s what the HTA code looks like:<\/P><PRE class=\"codeSample\">&lt;html&gt;\n&lt;head&gt;\n&lt;title&gt;HTA Test&lt;\/title&gt;\n&lt;HTA:APPLICATION \n     ID=&#8221;objTest&#8221; \n     APPLICATIONNAME=&#8221;HTA Test&#8221;\n     SCROLL=&#8221;yes&#8221;\n     SINGLEINSTANCE=&#8221;yes&#8221;\n&gt;\n&lt;\/head&gt;<\/p>\n<p>&lt;SCRIPT LANGUAGE=&#8221;VBScript&#8221;&gt;<\/p>\n<p>    Sub PrintText\n        strText = DataArea.Value<\/p>\n<p>        Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\n        strFileName = &#8220;temporary_print_file.txt&#8221;\n        Set objFile = objFSO.CreateTextFile(strFileName)\n        objFile.Write strText\n        objFile.Close<\/p>\n<p>        Set objFile = objFSO.GetFile(strFileName)\n        strPath = objFSO.GetParentFolderName(objFile)<\/p>\n<p>        Set objShell = CreateObject(&#8220;Shell.Application&#8221;)\n        Set objFolder = objShell.Namespace(strPath) \n        Set objFolderItem = objFolder.ParseName(strFileName)\n        objFolderItem.InvokeVerbEx(&#8220;Print&#8221;)<\/p>\n<p>    End Sub<\/p>\n<p>&lt;\/SCRIPT&gt;<\/p>\n<p>&lt;body&gt;\n    &lt;textarea name=&#8221;DataArea&#8221; rows=5 cols=70&gt;&lt;\/textarea&gt;&lt;p&gt;\n    &lt;input id=runbutton  type=&#8221;button&#8221; value=&#8221;Print&#8221; name=&#8221;run_button&#8221; onClick=&#8221;PrintText&#8221;&gt;\n&lt;\/body&gt;\n&lt;\/html&gt;\n<\/PRE>\n<TABLE id=\"EBE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Before we begin explaining how this all works we should note that we <I>won\u2019t<\/I> discuss the code involved in creating the HTA itself; our focus will be solely on the script that prints the information found in the text area. If you need some background information on creating HTAs then you might check out our <A href=\"http:\/\/null\/technet\/scriptcenter\/hubs\/htas.mspx\"><B>HTA Developer\u2019s Center<\/B><\/A>.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>So how do we print the information found in the text area? Well, we start by grabbing the information found in the text area and storing it in a variable named strText:<\/P><PRE class=\"codeSample\">strText = DataArea.Value\n<\/PRE>\n<P>After we\u2019ve done that we encounter a block of code that creates an instance of the <B>FileSystemObject<\/B> and provides a file name (temporary_print_file.txt) for our temporary print file. We then call the <B>CreateTextFile<\/B> method to create this file (by default it will be created in the same folder as the HTA itself). As soon as the file has been created we call the <B>Write<\/B> method to write the value of strText to the file, then close the file. All that takes place here:<\/P><PRE class=\"codeSample\">Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nstrFileName = &#8220;temporary_print_file.txt&#8221;\nSet objFile = objFSO.CreateTextFile(strFileName)\nobjFile.Write strText\nobjFile.Close\n<\/PRE>\n<P>What did that gain us? Well, we\u2019ve now taken all the information found in the text area and saved it to a separate text file. That means that, in order to print just the information in the text area, we don\u2019t have to do anything any more complicated than simply printing the text file. And that\u2019s what we do here:<\/P><PRE class=\"codeSample\">Set objFile = objFSO.GetFile(strFileName)\nstrPath = objFSO.GetParentFolderName(objFile)<\/p>\n<p>Set objShell = CreateObject(&#8220;Shell.Application&#8221;)\nSet objFolder = objShell.Namespace(strPath) \nSet objFolderItem = objFolder.ParseName(strFileName)\nobjFolderItem.InvokeVerbEx(&#8220;Print&#8221;)\n<\/PRE>\n<P>Yes, that looks a tiny bit complicated, but that\u2019s primarily because of the eccentricities of the Shell object: in particular, the Shell object requires us to take a slightly roundabout path before we can bind to &#8211; and then print &#8211; the file. But don\u2019t let the looks deceive you: if you\u2019ve seen <I>The Sixth Sense<\/I> then you know that things aren\u2019t always what they seem.<\/P>\n<P>In other words, we first use the <B>GetFile<\/B> method to bind the FileSystemObject to our temporary file. After that we call the <B>GetParentFolderName<\/B> method to determine the path to the folder where the temporary file lives (for example, if our temporary file is named C:\\Scripts\\temporary_print_file.txt then the parent folder will be C:\\Scripts). That\u2019s what the first two lines of code do:<\/P><PRE class=\"codeSample\">Set objFile = objFSO.GetFile(strFileName)\nstrPath = objFSO.GetParentFolderName(objFile)\n<\/PRE>\n<P>Once the folder path is stashed away in the variable strPath we can then create the Shell.Application object and use the <B>Namespace<\/B> method to bind to the folder. In turn, we can call the <B>ParseName<\/B> method on that folder, passing as the sole parameter the variable strFileName (which is storing the name of the temporary file for us). That\u2019s somewhat-goofy syntax, but the net result is that by binding to a folder and then passing ParseName the name of a file within that folder we get a <B>FolderItem<\/B> object pointing directly to our text file. Is that good? You bet it is: once the Shell object is bound to the file we can then use the <B>InvokeVerbEx<\/B> method to call any of the commands that appear on the context menu of that file.<\/P>\n<P>Is <I>that<\/I> good? Of course. If you right-click a .txt file, one of the commands available to you is <B>Print<\/B>; if you then click <B>Print<\/B> the file will automatically be printed to the default printer. As we noted, the InvokeVerbEx method enables us to run any of the commands found on the context menu of a file. To call the Print command, and thus automatically print the file to the default printer, all we need to do is execute this line of code:<\/P><PRE class=\"codeSample\">objFolderItem.InvokeVerbEx(&#8220;Print&#8221;)\n<\/PRE>\n<TABLE id=\"ENG\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. What if you don\u2019t like the way the text file prints out? In that case, try saving the file as temporary_print_file.htm and see if you like <I>that<\/I> printout any better.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>That\u2019s all we need to do. Once the file has been sent to the printer we\u2019re home free.<\/P>\n<P>Pretty cool, huh? We have to tell you, it\u2019s scripts like this one that make you glad to be alive.<\/P>\n<P>Well, for those of you who <I>are<\/I> alive, that is.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I have an HTA where information gets written to a text area. How can I print just the information found in that text area?&#8212; DS Hey, DS. We want you to level with us here: are the Scripting Guys dead? We ask that because we saw the movie The Sixth Sense a [&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":[40,3,4,707,5,30],"class_list":["post-67443","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-filesystemobject","tag-scripting-guy","tag-scripting-techniques","tag-shell-application","tag-vbscript","tag-web-pages-and-htas"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I have an HTA where information gets written to a text area. How can I print just the information found in that text area?&#8212; DS Hey, DS. We want you to level with us here: are the Scripting Guys dead? We ask that because we saw the movie The Sixth Sense a [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67443","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=67443"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/67443\/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=67443"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=67443"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=67443"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}