{"id":64883,"date":"2007-05-11T22:17:00","date_gmt":"2007-05-11T22:17:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/05\/11\/how-can-i-highlight-a-keyword-in-an-hta\/"},"modified":"2007-05-11T22:17:00","modified_gmt":"2007-05-11T22:17:00","slug":"how-can-i-highlight-a-keyword-in-an-hta","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-highlight-a-keyword-in-an-hta\/","title":{"rendered":"How Can I Highlight a Keyword in an HTA?"},"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! I\u2019d like to create an HTA that, upon loading, can open a text file and then display the contents. However, I\u2019d like a particular keyword in those contents to be highlighted. How can I do that?<BR><BR>&#8212; GD<\/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, GD. You know, a lot of people at Microsoft pooh-pooh the work that the Scripting Guys do. \u201cTake that <I>Hey, Scripting Guy!<\/I> column,\u201d they\u2019ll say. \u201cBig deal. I bet a <I>4-year-old <\/I>could write a column that good.\u201d Well, we\u2019d just like to say that those people are wrong. As it turns out, a 4-year-old could write a <I>better<\/I> column.<\/P>\n<P>Yesterday the Scripting Son had his wisdom teeth removed. (But don\u2019t worry; he didn\u2019t have all that much wisdom to lose in the first place.) In turn, the Scripting Dad had to stop at the pharmacy on his way home to pick up a prescription. While waiting his turn at the counter he ran into the wife and 4-year-old daughter of a guy he used to coach with. As it turned out, the wife and daughter had walked to the drugstore, about a mile or so from their house.<\/P>\n<P>The Scripting Guy and the Coach\u2019s Wife chatted for a moment, with the Coach\u2019s Wife noting that her daughter didn\u2019t really enjoy the walk. \u201cIt\u2019s such a nice day I thought a walk would be fun,\u201d said the wife. \u201cBut the little one doesn\u2019t seem to agree with me. She kept saying it was too far to walk.\u201d<\/P>\n<P>It was at that very moment that the little one spoke up. \u201cMommy, can Coach Greg give us a ride home?\u201d she asked. \u201cMy legs are bored.\u201d<\/P>\n<P><I>My legs are bored<\/I>! The Scripting Guys would <I>kill<\/I> to be able to write something that good.<\/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 class=\"lastInCell\"><B>Note<\/B>. That <I>is<\/I> just a figure of speech, right? I mean, the Scripting Guys wouldn\u2019t really <I>kill<\/I> just to be able to write a cute phrase, would they? Would they? Hello?<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Oh, well; that\u2019s not the first time we\u2019ve been bested by a 4-year-old. We\u2019ll go ahead and answer your question, GD. It won\u2019t be anywhere near as cute and clever as \u201cMy legs are bored.\u201d However, it <I>will<\/I> have a little more scripting code:<\/P><PRE class=\"codeSample\">&lt;Script Language=&#8221;VBScript&#8221;&gt;\n    Sub Window_Onload\n        Const ForReading = 1<\/p>\n<p>        Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\n        Set objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;)<\/p>\n<p>        strContent = objFile.ReadAll\n        objFile.Close<\/p>\n<p>        strNewText = &#8220;&lt;span style=&#8217;background-color: #FFFF00&#8242;&gt;text&lt;\/span&gt;&#8221;\n        strContent = Replace(strContent, &#8220;text&#8221;, strNewText)\n        DataArea.InnerHTML = strContent\n    End Sub\n&lt;\/script&gt;<\/p>\n<p>&lt;body&gt;\n    &lt;span id = &#8220;DataArea&#8221;&gt;&lt;\/span&gt;\n&lt;\/body&gt;\n<\/PRE>\n<P>As you can see, we have a very simple HTA here. (If you don\u2019t know what HTAs are, then take a look at our <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/hubs\/htas.mspx\"><B>HTA Developers Zone<\/B><\/A>.) This simple HTA consists of two parts: the &lt;BODY&gt; tag and a single subroutine (enclosed, as all good subroutines are, inside the &lt;SCRIPT&gt; tag). It goes without saying that there\u2019s not much to the &lt;BODY&gt; tag; in fact, all we have there is a &lt;SPAN&gt; named DataArea:<\/P><PRE class=\"codeSample\">&lt;span id = &#8220;DataArea&#8221;&gt;&lt;\/span&gt;\n<\/PRE>\n<P>If you\u2019re new to HTML tagging, a &lt;SPAN&gt; is simply a named area of an HTML file (or an HTA file). By setting aside a portion of the document and giving it a name, we can refer to \u2013 and modify \u2013 that section of the document using a little scripting code. It should come as no surprise, then, that this is exactly what our subroutine is going to do.<\/P>\n<P>Speaking of our subroutine, you might have noticed that we gave it the name <B>Window_Onload<\/B>. That\u2019s not a name we picked because it was, say, our grandfather\u2019s name. (In case you\u2019re wondering, no, it wasn\u2019t.) Instead, any subroutine that has the name Window_Onload automatically runs each time the HTA is loaded or refreshed. GD needs an HTA that, on its own initiative, goes ahead and opens a text file each time it gets started. How do we do that? We just put the file-opening code into a subroutine named Window_Onload.<\/P>\n<P>Which, now that we think about it, is <I>exactly<\/I> what we did. We\u2019d like to see a 4-year-old do <I>that<\/I>!<\/P>\n<P>Inside our subroutine we start out by defining a constant named ForReading and setting the value to 1; as the name implies, we\u2019ll use this constant when we go to open, and read, the text file. We then use these two lines of code to create an instance of the <B>Scripting.FileSystemObject<\/B> object and open the file C:\\Scripts\\Test.txt for reading:<\/P><PRE class=\"codeSample\">Set objFSO = CreateObject(&#8220;Scripting.FileSystemObject&#8221;)\nSet objFile = objFSO.OpenTextFile(&#8220;C:\\Scripts\\Test.txt&#8221;)\n<\/PRE>\n<P>As soon as the file is open we use the <B>ReadAll<\/B> method to read in the entire file and store the contents in a variable named strContent:<\/P><PRE class=\"codeSample\">strContent = objFile.ReadAll\n<\/PRE>\n<P>And then once we have the contents safely stashed away in memory we go ahead and close the file:<\/P><PRE class=\"codeSample\">objFile.Close\n<\/PRE>\n<P>That part was pretty easy, wasn\u2019t it? And don\u2019t worry: it doesn\u2019t get much harder, either.<\/P>\n<P>We\u2019re assuming that our text file looks something like this:<\/P><PRE class=\"codeSample\">Here is some sample text taken from the file C:\\Scripts\\Test.txt. The idea is to take this text \nand then highlight each instance of the word text. Like this one: text.\n<\/PRE>\n<P>If we wanted to, we could simply set the <B>InnerHTML <\/B>property of our &lt;SPAN&gt; to the value of strContent; that would result in the contents of the text file being displayed in our HTA. But that\u2019s something a 4-year-old would do. A 74- &#8212; um, we mean a <I>24<\/I>-year-old, like the Scripting Guy who writes this column, would go one step further: he would highlight a target word anywhere it appeared in the text. How\u2019s he going to do that? We\u2019re just about to show you.<\/P>\n<P>The InnerHTML property is a very cool property. Why? Because it not only allows you to assign text to a &lt;SPAN&gt; but it also allows you to assign HTML formatting to a &lt;SPAN&gt;. If you want to highlight a single word using HTML tags, one way to do that is to turn that single word into a sort of mini-&lt;SPAN&gt;, then assign that mini-&lt;SPAN&gt; its own background color. For example, suppose we wanted to highlight the word <I>text<\/I>. In that case, we could use HTML tagging similar to this:<\/P><PRE class=\"codeSample\">&lt;span style=&#8217;background-color: #FFFF00&#8242;&gt;text&lt;\/span&gt;\n<\/PRE>\n<P>And guess what? That\u2019s exactly what we\u2019re going to do. For starters, we take the preceding HTML tagging and assign it to a variable named strNewText:<\/P><PRE class=\"codeSample\">strNewText = &#8220;&lt;span style=&#8217;background-color: #FFFF00&#8242;&gt;text&lt;\/span&gt;&#8221;\n<\/PRE>\n<P>We then use this line of code and the <B>Replace<\/B> function to search through the value of strContent and replace all instances of the word <I>text<\/I> with our HTML tagging (which simply displays the word <I>text<\/I> on a yellow background):<\/P><PRE class=\"codeSample\">strContent = Replace(strContent, &#8220;text&#8221;, strNewText)\n<\/PRE>\n<P>That makes the value of strContent equal to this:<\/P><PRE class=\"codeSample\">Here is some sample &lt;span style=&#8217;background-color: #FFFF00&#8242;&gt;text&lt;\/span&gt; taken from the file C:\\Scripts\\Test.txt. \nThe idea is to take this &lt;spanstyle=&#8217;background-color: #FFFF00&#8242;&gt;text&lt;\/span&gt; and then highlight each instance of the word \n&lt;span style=&#8217;background-color: #FFFF00&#8242;&gt;text&lt;\/span&gt;. Like this one: &lt;span style=&#8217;background-color: #FFFF00&#8242;&gt;text&lt;\/span&gt;.\n<\/PRE>\n<P><I>Now<\/I> we can go ahead and assign the value of strContent to the InnerHTML property:<\/P><PRE class=\"codeSample\">DataArea.InnerHTML = strContent\n<\/PRE>\n<P>Lo and behold, not only does our HTA display the contents of the file C:\\Scripts\\Test.txt, but every instance of the target word is highlighted:<\/P><IMG height=\"229\" alt=\"HTA\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/highlight2.jpg\" width=\"358\" border=\"0\"> \n<P><BR>Is that cool or what?<\/P>\n<P>We hope that answers your question, GD. Now we just need a socko-boffo ending for today\u2019s column, something along the lines of, \u201cCan we go now? My legs are bored.\u201d Hmmm \u2026.<\/P>\n<P>What\u2019s that? You say you have a suggestion: \u201cCan we go now? Your <I>readers<\/I> are bored.\u201d Oh, man; that was uncalled for!<\/P>\n<P>On the other hand, we can\u2019t really think of anything better, so we\u2019ll go with that. See you all Monday.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I\u2019d like to create an HTA that, upon loading, can open a text file and then display the contents. However, I\u2019d like a particular keyword in those contents to be highlighted. How can I do that?&#8212; GD Hey, GD. You know, a lot of people at Microsoft pooh-pooh the work that the [&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-64883","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! I\u2019d like to create an HTA that, upon loading, can open a text file and then display the contents. However, I\u2019d like a particular keyword in those contents to be highlighted. How can I do that?&#8212; GD Hey, GD. You know, a lot of people at Microsoft pooh-pooh the work that the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64883","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=64883"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/64883\/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=64883"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=64883"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=64883"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}