{"id":56243,"date":"2008-02-08T22:45:00","date_gmt":"2008-02-08T22:45:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/02\/08\/hey-scripting-guy-how-can-i-select-text-in-a-hta-and-then-append-that-text-to-a-file\/"},"modified":"2008-02-08T22:45:00","modified_gmt":"2008-02-08T22:45:00","slug":"hey-scripting-guy-how-can-i-select-text-in-a-hta-and-then-append-that-text-to-a-file","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-select-text-in-a-hta-and-then-append-that-text-to-a-file\/","title":{"rendered":"Hey, Scripting Guy! How Can I Select Text in a HTA and Then Append That Text to a File?"},"content":{"rendered":"<p><img decoding=\"async\" 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\" \/> <\/p>\n<p>Hey, Scripting Guy! I read <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/feb08\/hey0204.mspx\"><strong>your column<\/strong><\/a> about the HTA that could load and then display all the files in a folder. That was pretty useful, but I had an idea to make it even better. When looking at a log file I often see portions of the log that I&rsquo;d like to copy and save to a separate text file. Is there any way to highlight text in an HTA and then append just the highlighted text to another file?<\/p>\n<p>&#8212; KF<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\" \/><img decoding=\"async\" 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 decoding=\"async\" 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><\/p>\n<p>Hey, KF. Well, today is Friday, February 8<sup>th<\/sup>, which means that the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/default.mspx\"><strong>2008 Winter Scripting Games<\/strong><\/a> are just one week away. As exciting as that is to most of us, however, we realize that some people aren&rsquo;t interested in the Scripting Games. (Now, now; remember, it takes all kinds to make a world.) Therefore, we thought we&rsquo;d talk about something different today.<\/p>\n<p>So let&rsquo;s see, what can we talk about? Well, politics are definitely in the news these says, especially here in the US. And as far as the Scripting Guys are concerned, Americans would be far better off if &ndash; well, never mind. Microsoft doesn&rsquo;t really like us to talk about politics. (Actually Microsoft doesn&rsquo;t really like the Scripting Guys to talk about <i>anything<\/i>. But that&rsquo;s a different story.)<\/p>\n<p>So what else? Well, there&rsquo;s always religion, although, come to think of it, we&rsquo;re not supposed to talk about religion, either.<\/p>\n<table class=\"dataTable\" id=\"ERD\" cellspacing=\"0\" cellpadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" valign=\"top\">\n<td>\n<p class=\"lastInCell\"><strong>True story<\/strong>. In a recent magazine article, we weren&rsquo;t even allowed to say the word &ldquo;devil.&rdquo; Which was a bit of a problem seeing as how we had just returned from the Devil&rsquo;s Punch Bowl and were <i>dying <\/i>to have some deviled eggs and a piece of Devil&rsquo;s food cake. Needless to say, that also played havoc with our proposed tribute column to everyone&rsquo;s favorite hockey player, Miroslav Satan.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Anyway, religion is out. Sports? Well, to tell you the truth, the Scripting Guy who writes this column is going through a bit of a dry spell as far as his favorite teams are concerned; none of them are doing very well at the moment. (Well, the New Jersey Devils and the Arizona State Sun Devils are doing all right, but we can&rsquo;t talk about them.) In other words, he doesn&rsquo;t want to talk about sports. OK, then how about the weather? Well, today in Seattle it&rsquo;s cold and rainy; tomorrow is also supposed to be cold and rainy. And the day after that is <i>also<\/i> supposed to be cold and rainy. And &ndash; well, let&rsquo;s not talk about the weather, either. OK? <\/p>\n<p>In fact, why don&rsquo;t we dispense with the small talk and just show you code for an HTML Application (HTA) that allows you to select text in a textarea and then append the selected information to a text file:<\/p>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">&lt;SCRIPT Language=\"VBScript\"&gt;   &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">Sub Window_Onload       &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">strComputer = \".\"       &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">Set objWMIService = GetObject(\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")       &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">Set colFiles = objWMIService.ExecQuery _           &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">(\"ASSOCIATORS OF {Win32_Directory.Name='C:\\Scripts'} Where \" _               &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">&amp; \"ResultClass = CIM_DataFile\")      &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\"> For Each objFile In colFiles           &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">Set objOption = Document.createElement(\"OPTION\")           &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">objOption.Text = objFile.FileName           &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">objOption.Value = objFile.Name          &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">LogFiles.Add(objOption)       &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">Next   &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">End Sub   &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">Sub ReadFile       &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">Set objFSO = CreateObject(\"Scripting.FileSystemObject\")       &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">Set objFile = objFSO.OpenTextFile(LogFiles.Value)      &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\"> strContents = objFile.ReadAll       &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">objFile.Close       &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">BasicTextArea.Value = strContents   &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">End Sub       &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">Sub SaveSelection       &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">Set objSelection = Document.Selection.CreateRange()       &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">Set objFSO = CreateObject(\"Scripting.FileSystemObject\")       &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">Set objFile = objFSO.OpenTextFile(FileNameBox.Value, 8)       &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">objFile.WriteLine       &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">objFile.WriteLine (LogFiles.Value)       &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">objFile.WriteLine objSelection.Text       &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">objFile.Close    End Sub&lt;\/SCRIPT&gt;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">&lt;body&gt;   &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">&lt;table width=\"100%\"&gt;       &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">&lt;tr&gt;           &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">&lt;td width=\"25%\" valign=\"top\"&gt;              &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">&lt;select size=\"35\" name=\"LogFiles\" onChange=\"ReadFile\" style=\"width:150px\"&gt;                   &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">&lt;\/select&gt;           &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">&lt;\/td&gt;          &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">&lt;td width=\"75%\" valign=\"top\"&gt;              &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">&lt;textarea name=\"BasicTextArea\" rows=\"35\" cols=\"100\"&gt;&lt;\/textarea&gt;          &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">&lt;\/td&gt;       &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">&lt;\/tr&gt;       &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">&lt;tr&gt;           &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">&lt;td width=\"25%\" valign=\"top\"&gt;              &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">File name: &lt;input type=\"text\" size=\"25\" name=\"FileNameBox\" value=\"C:\\Scripts\\Test.txt\"&gt;            &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">&lt;\/td&gt;          &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">&lt;td width=\"75%\" valign=\"top\"&gt;              &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">&lt;br&gt;&lt;input type=\"button\" value=\"Append Data\" onClick=\"SaveSelection\"&gt;         &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">&lt;\/td&gt;       &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">&lt;\/tr&gt;   &nbsp;<\/span><\/pre>\n<pre class=\"codeSample\"><span style=\"font-size: x-small\">&lt;\/table&gt;&lt;\/body&gt;<\/span><\/pre>\n<p>Wow, that <i>is<\/i> quite a bit of code, at least for a <i>Hey, Scripting Guy!<\/i> column. But don&rsquo;t worry; we aren&rsquo;t going to walk through each and every line today. Instead, we&rsquo;ll refer you to our <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/feb08\/hey0204.mspx\"><strong>previous column<\/strong><\/a> to explain how the HTA loads all the files in a folder into a list box, then displays the contents of any file you select from that list box. All we&rsquo;ll talk about today is the code that we added to our original HTA, code that enables you to select text in the HTA&rsquo;s textarea, then append that selection to a separate text file.<\/p>\n<p>With that in mind, let&rsquo;s start by taking a look at the all-new second row in our table. (The first row in the table contains the list box and the textarea.) Here&rsquo;s the HTML tagging for row 2 in the table:<\/p>\n<pre class=\"codeSample\">&lt;tr&gt;   &nbsp;<\/pre>\n<pre class=\"codeSample\">&lt;td width=\"25%\" valign=\"top\"&gt;       &nbsp;<\/pre>\n<pre class=\"codeSample\">File name: &lt;input type=\"text\" size=\"25\" name=\"FileNameBox\" value=\"C:\\Scripts\\Test.txt\"&gt;   &nbsp;<\/pre>\n<pre class=\"codeSample\"> &lt;\/td&gt;   &nbsp;<\/pre>\n<pre class=\"codeSample\">&lt;td width=\"75%\" valign=\"top\"&gt;       &nbsp;<\/pre>\n<pre class=\"codeSample\">&lt;br&gt;<\/pre>\n<pre class=\"codeSample\">&lt;input type=\"button\" value=\"Append Data\" onClick=\"SaveSelection\"&gt;   &nbsp;<\/pre>\n<pre class=\"codeSample\">&lt;\/td&gt;<\/pre>\n<pre class=\"codeSample\">&lt;\/tr&gt;<\/pre>\n<p>In the first cell in this table we&rsquo;re adding a text box named FileNameBox, and setting the default <strong>value<\/strong> of this text box to C:\\Scripts\\Test.txt. That&rsquo;s what this little bit of HTML is for:<\/p>\n<pre class=\"codeSample\">&lt;input type=\"text\" size=\"25\" name=\"FileNameBox\" value=\"C:\\Scripts\\Test.txt\"&gt;<\/pre>\n<p>Why did we add a text box here? Well, this gives us a place where we can type the path to the file where the selected text is supposed to be added. We give it a default value of C:\\Scripts\\Test.txt because we&rsquo;re assuming that this is the file where we typically want to append information. However, it&rsquo;s easy enough to redirect that data to a different file; all we have to do is type a different file path in our text box.<\/p>\n<p>In the second cell in the row we use the following HTML tagging to add a button labeled <strong>Append Data<\/strong>:<\/p>\n<pre class=\"codeSample\">&lt;input type=\"button\" value=\"Append Data\" onClick=\"SaveSelection\"&gt;<\/pre>\n<p>As you can see, each time the button is clicked the script runs a subroutine named SaveSelection. Now, try to guess what&rsquo;s going to happen when the SaveSelection subroutine runs?<\/p>\n<p>Oh, well, you&rsquo;re right: we <i>are<\/i> going to save the selected text to a text file. We thought we might be able to stump you with that particular question. Apparently we were wrong.<\/p>\n<p>Well, even though you already know what the SaveSelection subroutine does, let&rsquo;s take a moment to talk about <i>how<\/i> it does this. The first thing we do inside this subroutine is create a new <strong>TextRange<\/strong> object; that&rsquo;s done by calling the <strong>CreateRange()<\/strong> method, which happens to be part of the <strong>Document.Selection<\/strong> object: <\/p>\n<pre class=\"codeSample\">Set objSelection = Document.Selection.CreateRange()<\/pre>\n<p>After creating the TextRange object (which automatically encompasses all the selected text in the HTA) we use these two lines of code to create an instance of the <strong>Scripting.FileSystemObject<\/strong> and then open a text file for appending (that&rsquo;s what the second parameter, the <strong>8<\/strong>, is for):<\/p>\n<pre class=\"codeSample\">Set objFSO = CreateObject(\"Scripting.FileSystemObject\")<\/pre>\n<pre class=\"codeSample\">Set objFile = objFSO.OpenTextFile(FileNameBox.Value, 8)<\/pre>\n<p>What&rsquo;s that? <i>Which<\/i> text file are we opening? Why, we&rsquo;re opening the file whose path is listed in the FileNameBox text box; as you might have guessed, the syntax <strong>FileNameBox.Value<\/strong> returns the text found in the FileNameBox text box. <\/p>\n<p>So what are we going to do with that file now that it&rsquo;s open? Well, to begin with, we&rsquo;re going to use this line of code to add a blank line to the end of the file:<\/p>\n<pre class=\"codeSample\">objFile.WriteLine<\/pre>\n<p>Once that&rsquo;s done we&rsquo;re going to use this block of code to write the name of the file that the selection comes from (grabbing the file path from the list box) followed by the selected text, something we can get at by referencing the <strong>Text<\/strong> property of our TextRange object:<\/p>\n<pre class=\"codeSample\">objFile.WriteLine (LogFiles.Value)<\/pre>\n<pre class=\"codeSample\">objFile.WriteLine objSelection.Text<\/pre>\n<p>And then we close the file and simply wait for the user to select some more text and then click the <strong>Append Data<\/strong> button. That&rsquo;s all we have to do.<\/p>\n<p>We hope that answers your question, KF. In the meantime &hellip; wow, there just really <i>isn&rsquo;t<\/i> anything to talk about other than the Scripting Games, is there? So what the heck, let&rsquo;s just mention that the Games begin February 15<sup>th<\/sup> and run through March 3<sup>rd<\/sup>. However, you&rsquo;ll want to show up on the 15<sup>th<\/sup> if at all possible: although the Games run through March 3<sup>rd<\/sup> most of the events will end <i>before<\/i> March 3<sup>rd<\/sup>. (For example, the deadline for Events 1 and 2 is Wednesday, February 20<sup>th<\/sup>. Check <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/games08\/schedule.mspx\"><strong>here<\/strong><\/a> for a full schedule, including a printable calendar.) <\/p>\n<p>But even if you can&rsquo;t make the deadlines for all the events, well, that doesn&rsquo;t mean you can&rsquo;t participate: just do whatever events you have time for and call it good. You&rsquo;ll have fun and you&rsquo;ll have a chance to put your scripting skills to the test (and maybe learn something new along the way). Equally important, by entering just one event you&rsquo;ll be eligible for great <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/games08\/prizes.mspx\"><strong>Scripting Games prizes<\/strong><\/a>. And remember, you don&rsquo;t have to use your real name when playing. (Surely you didn&rsquo;t think someone <i>really<\/i> named their kid N3QGO or weedwhacker?) Try a script, send it in using a nickname like weedwhacker (well, maybe not weedwhacker; that&rsquo;s already taken), and who knows? You just might win one of those great prizes.<\/p>\n<p>Now, aren&rsquo;t you glad we had this little talk?<\/p>\n<table class=\"dataTable\" id=\"EDAAC\" cellspacing=\"0\" cellpadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" valign=\"top\">\n<td>\n<p class=\"lastInCell\"><strong>Note<\/strong>. Now that you mention it, the Scripting Editor <i>did<\/i> leave a few doughnut crumbs in the office, didn&rsquo;t she? But that&rsquo;s no big deal, we&rsquo;ll just get out our trusty little Dirt Devil vacuum and &ndash; well, never mind. We&rsquo;ll just ask the janitor to take care of it.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I read your column about the HTA that could load and then display all the files in a folder. That was pretty useful, but I had an idea to make it even better. When looking at a log file I often see portions of the log that I&rsquo;d like to copy and [&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":[3,4,14,5,30],"class_list":["post-56243","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-text-files","tag-vbscript","tag-web-pages-and-htas"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I read your column about the HTA that could load and then display all the files in a folder. That was pretty useful, but I had an idea to make it even better. When looking at a log file I often see portions of the log that I&rsquo;d like to copy and [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/56243","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=56243"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/56243\/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=56243"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=56243"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=56243"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}