{"id":65943,"date":"2006-11-30T16:02:00","date_gmt":"2006-11-30T16:02:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/11\/30\/how-can-i-read-the-individual-lines-in-a-multi-line-text-box-in-a-hta\/"},"modified":"2006-11-30T16:02:00","modified_gmt":"2006-11-30T16:02:00","slug":"how-can-i-read-the-individual-lines-in-a-multi-line-text-box-in-a-hta","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-read-the-individual-lines-in-a-multi-line-text-box-in-a-hta\/","title":{"rendered":"How Can I Read the Individual Lines in a Multi-Line Text Box in a HTA?"},"content":{"rendered":"<p><img decoding=\"async\" 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\"><\/p>\n<p>Hey, Scripting Guy! In an HTA, how can I read the individual lines in a multi-line text box?<\/p>\n<p>&#8212; KM<\/p>\n<p><img decoding=\"async\" border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><img decoding=\"async\" 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 decoding=\"async\" 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><\/p>\n<p>Hey, KM. You know, to be perfectly honest the Scripting Guy who writes this column has always had a grudging admiration for people who do totally ridiculous things and yet somehow manage to get away with them. For example, in the past couple weeks a large amount of the spam mail he has received has been exactly the same: it purports to be a \u201csecret and confidential\u201d message from the CEO of some company. These emails were obviously not intended for the Scripting Guy who writes this column; instead, the CEOs must have inadvertently addressed them to the Scripting Guy rather than their fellow inside traders. (We hate when that happens: you sit down to type kenmyer@fabrikam.com and you end up typing pilar.ackerman@contoso.org instead.)<\/p>\n<p>As it turns out, these secret and confidential messages were all supposed to clue the CEOs\u2019 cronies in on some incredible deals: thanks to a leveraged buyout\/dramatic breakthrough\/government contract the stock of some company no one has ever heard of is about to go through the roof. And now that you have this inside tip, all you need to do is buy the stock, sit back, and wait for the money to roll in. Thank goodness CEOs don\u2019t know how to use email, eh?<\/p>\n<table id=\"E3C\" 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>. Interestingly enough, these CEOs all seem to have email names like way_2_cool_dude. But maybe if you\u2019re the CEO of some major company you <i>are<\/i> a way too-cool dude.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>The best part is that the company mentioned in the email is a real company and one whose stock is priced very low. We assume that the spammer must own a bunch of shares of this company\u2019s stock, and is trying to entice as many people as he or she can to buy additional shares. In turn, that will drive the price up, at which point the spammer likely sells their shares and starts over again with another company. We don\u2019t know for sure if that\u2019s what\u2019s going on; after all, maybe way_2_cool_dude really <i>is<\/i> the CEO of some company. But that sounds like a pretty good way to scam people. (Hmmm \u2026 wonder if anyone has patented that idea \u2026.) Admittedly, it\u2019s hard to believe that literally scores of CEOs would accidentally send you secret and confidential emails on the very same day. But, then again, if you\u2019ve ever seen the people who run some of our major companies, well \u2026.<\/p>\n<p>At any rate, it all sounds ridiculous, but apparently it works, and works well. <\/p>\n<p>Speaking of which, we Scripting Guys are well aware that there\u2019s no way to read the individual lines in a multi-line text box. However \u2013 and please keep this under your hat, as it\u2019s both secret <i>and<\/i> confidential \u2013 we happen to know of a way that can help you achieve the same effect. Just between us, you might want to consider stocking up on HTML Applications (HTAs); when word of this leaks out the number of people writing HTAs is going to go through the roof (which means you might want to buy stock in roofing companies while you\u2019re at it):<\/p>\n<pre class=\"codeSample\">&lt;SCRIPT LANGUAGE=\"VBScript\"&gt;\nSub TestSub\n    strText = MyTextArea.Value\n    arrLines = Split(strText, vbCrLf)\n    For Each strLine in arrLines\n        Msgbox strLine\n    Next\nEnd Sub\n&lt;\/SCRIPT&gt;\n&lt;body&gt;\n    &lt;textarea name=\"MyTextArea\" rows=5 cols=70&gt;&lt;\/textarea&gt;&lt;p&gt;\n    &lt;input type=\"button\" value=\"Run Script\" onClick=\"TestSub\"&gt;\n&lt;\/body&gt;\n<\/pre>\n<p>As you can see (and only you; remember, this is secret <i>and<\/i> confidential) this is an extremely simple HTA, consisting primarily of a multi-line text box named <i>MyTextArea<\/i> (technically, a multi-line text box is known as a <b>text area<\/b>) and a button labeled <i>Run Script.<\/i> The idea is that you type some information into the text area and then click the button; when the button is clicked it then runs a subroutine named <i>TestSub<\/i>. In turn, that subroutine reports back the value of each individual line in the text area.<\/p>\n<p>By the way, here\u2019s the HTML tagging that puts a text area (5 rows high by 70 columns wide) and a button in our HTA:<\/p>\n<pre class=\"codeSample\">&lt;textarea name=\"MyTextArea\" rows=5 cols=70&gt;&lt;\/textarea&gt;&lt;p&gt;\n&lt;input type=\"button\" value=\"Run Script\" onClick=\"TestSub\"&gt;\n<\/pre>\n<p>What\u2019s that? Well, you\u2019re right: we <i>did<\/i> say that there was no way to read individual lines in a multi-line text box, didn\u2019t we? And that\u2019s true; there isn\u2019t. Therefore, we\u2019re going to cheat a little here. We\u2019re assuming that after you type information into the text box you then press ENTER to go to the next line. In other words, you\u2019re really typing in a list of information as opposed to a paragraph of information For example, maybe each line in your text box consists of a user name:<\/p>\n<pre class=\"codeSample\">Ken Myer\nPilar Ackerman\nJonathan Haas\n<\/pre>\n<p>If that\u2019s true, then we can separate the individual lines (that is, the individual user names) by using the following subroutine:<\/p>\n<pre class=\"codeSample\">Sub TestSub\n    strText = MyTextArea.Value\n    arrLines = Split(strText, vbCrLf)\n    For Each strLine in arrLines\n        Msgbox strLine\n    Next\nEnd Sub\n<\/pre>\n<p>What we\u2019re doing here is first grabbing <i>all<\/i> the text in the text box and stashing it in a variable named strText; that\u2019s what this line of code is for:<\/p>\n<pre class=\"codeSample\">strText = MyTextArea.Value\n<\/pre>\n<p>After we\u2019ve done that we use the VBScript <b>Split<\/b> function to convert the data in strText to an array named arrLines:<\/p>\n<pre class=\"codeSample\">arrLines = Split(strText, vbCrLf)\n<\/pre>\n<p>As you can see, we pass two parameters to the Split function: the value we want to convert to an array and the character we want to \u201csplit\u201d on. (In other words, which character do we use to mark the end of one item in the array and then beginning of the next item?) Because we pressed the ENTER key after each line, we want to split on the carriage return-linefeed character, something we specify using the VBScript constant <b>vbCrLf<\/b>.<\/p>\n<p>What will that do for us? That will give us an array named arrLines, consisting of the following items:<\/p>\n<pre class=\"codeSample\">Ken Myer\nPilar Ackerman\nJonathan Haas\n<\/pre>\n<p>And you\u2019re absolutely right: that\u2019s an array in which each item represents an individual line in the text box.<\/p>\n<p>At this point, of course, you can do anything you want with this information. Just for the sake of doing <i>something<\/i> we simply set up a For Each loop and echo back the value of each line in the text box:<\/p>\n<pre class=\"codeSample\">For Each strLine in arrLines\n    Msgbox strLine\nNext\n<\/pre>\n<p>We\u2019re assuming you\u2019ll come up with a task that\u2019s a little more interesting, and a little more useful.<\/p>\n<p>Incidentally, you need the For Each loop only if you want to read every line in the text box. What if you wanted to read only, say, line 2? No problem. After all, each line is just an element in an array; to get the value of a specific line all we have to do is reference the appropriate index number. Because the first item in an array has the index number 0, that means the second item (the second line) has an index number of 1. This single line of code echoes back the value for the second line in the text box:<\/p>\n<pre class=\"codeSample\">Msgbox arrLines(1)\n<\/pre>\n<p>Again, keep in mind that all this works only if you\u2019ve pressed the ENTER key at the end of each line. Suppose you don\u2019t do that, suppose you type in a paragraph that looks like this:<\/p>\n<pre class=\"codeSample\">This is a paragraph typed into a text area. Instead of typing ENTER\nat the end of each line we simply let the text wrap. That means there\u2019s\nreally only one line in this text box.\n<\/pre>\n<p>If you run click the <b>Run Script<\/b> button the subroutine will simply echo back the entire paragraph; that\u2019s because, as far as the subroutine is concerned, there\u2019s only one line in the text box. Is it possible to somehow parse each line in the text box <i>without<\/i> using the ENTER key? Well, we can\u2019t say with 100% certainty that it <i>can\u2019t<\/i> be done. But for all practical purposes, no, it can\u2019t be done. <\/p>\n<p>Why not? Well, for one thing, suppose you had a text box that resized itself any time the HTA was resized. In that case the value of each \u201cline\u201d would change depending on the width of the text box. Of course, even in this case the vbCrLf trick would still work because we\u2019re splitting on the carriage return-linefeed, regardless of how many lines <i>appear<\/i> to be in the text box.<\/p>\n<p>Not a perfect solution by any means, but we hope it helps, KM. Meanwhile, now that people know how to read individual lines in a text area, well, the Scripting Guy who writes this column is going to just sit back and wait for the money to start rolling in.<\/p>\n<table id=\"EBG\" 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>. Well, no, he\u2019s not exactly sure <i>how<\/i> he\u2019s going to make money off this. Why, do you think he should have thought of that before writing the column and trying to pull off a scam like this one?<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! In an HTA, how can I read the individual lines in a multi-line text box? &#8212; KM Hey, KM. You know, to be perfectly honest the Scripting Guy who writes this column has always had a grudging admiration for people who do totally ridiculous things and yet somehow manage to get away [&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,5,30],"class_list":["post-65943","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-web-pages-and-htas"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! In an HTA, how can I read the individual lines in a multi-line text box? &#8212; KM Hey, KM. You know, to be perfectly honest the Scripting Guy who writes this column has always had a grudging admiration for people who do totally ridiculous things and yet somehow manage to get away [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65943","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=65943"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/65943\/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=65943"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=65943"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=65943"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}