{"id":68233,"date":"2006-01-06T08:25:00","date_gmt":"2006-01-06T08:25:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/01\/06\/how-can-i-determine-which-text-has-been-selected-in-an-hta\/"},"modified":"2006-01-06T08:25:00","modified_gmt":"2006-01-06T08:25:00","slug":"how-can-i-determine-which-text-has-been-selected-in-an-hta","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-which-text-has-been-selected-in-an-hta\/","title":{"rendered":"How Can I Determine Which Text has been Selected in 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! How can I determine which text has been selected in an HTA?<BR><BR>&#8212; DO<\/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, DO. You know, most people think that writing the <I>Hey, Scripting Guy!<\/I> column every day is one of the most glamorous and exciting jobs in the world. And it probably would be if it wasn\u2019t for the fact that the Scripting Guy who writes this column is so lazy. For example, lots of times he selects questions that he <I>knows<\/I> can be scripted. He usually doesn\u2019t have a script on-hand that carries out the task in question, but he knows that it\u2019s possible to write such a script. That\u2019s an efficient way to do things, but not a particularly exciting way to do things.<\/P>\n<P>This question was a little different, however. How can you determine which text has been selected in an HTA? Heck, we weren\u2019t even sure if you <I>can<\/I> determine which text has been selected in an HTA. But it turns out that you can:<\/P><PRE class=\"codeSample\">&lt;html&gt;\n&lt;head&gt;\n    &lt;title&gt;HTA Test&lt;\/title&gt;\n&lt;\/head&gt;<\/p>\n<p>&lt;SCRIPT LANGUAGE=&#8221;VBScript&#8221;&gt;\n    Sub ShowSelection\n        Set objSelection = Document.Selection.CreateRange()\n        Msgbox objSelection.text\n    End Sub\n&lt;\/SCRIPT&gt;<\/p>\n<p>&lt;body&gt;\n     &lt;textarea name=&#8221;ScriptArea&#8221; rows=5 cols=40&gt;&lt;\/textarea&gt;&lt;p&gt;\n     &lt;input id=runbutton  type=&#8221;button&#8221; value=&#8221;Show Selection&#8221; onClick=&#8221;ShowSelection&#8221;&gt;\n&lt;\/body&gt;\n<\/PRE>\n<P>What we have here is a simple little HTA that looks something like this:<\/P><IMG border=\"0\" alt=\"HTA\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/select1.jpg\" width=\"392\" height=\"249\"> \n<P><BR>What\u2019s that? How could you <I>not<\/I> be impressed? Well, try this. Type some text in the text area and then highlight a portion of that text:<\/P><IMG border=\"0\" alt=\"HTA\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/select2.jpg\" width=\"392\" height=\"249\"> \n<P><BR>Now click the button labeled <B>Show Selection<\/B>. With any luck at all you\u2019ll get a message box telling you which text has been selected:<\/P><IMG border=\"0\" alt=\"HTA\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/qanda\/select3.jpg\" width=\"104\" height=\"107\"> \n<P><BR>You\u2019re right: that\u2019s a bit more like it.<\/P>\n<P>So how does this all work? Well, we start off by creating an HTA that includes a text area and a button. We won\u2019t discuss the details behind creating the HTA itself; if you need a little background information check out the <A href=\"http:\/\/null\/technet\/scriptcenter\/hubs\/htas.mspx\"><B>HTA Developers Center<\/B><\/A> or our <A href=\"http:\/\/msevents.microsoft.com\/CUI\/EventDetail.aspx?EventID=1032282307&amp;Culture=en-US\" target=\"_blank\"><B>Scripting Week 3 webcast on HTAs<\/B><\/A><B>.<\/B> Today we\u2019re just going to focus on the subroutine that runs when you click the button:<\/P><PRE class=\"codeSample\">Sub ShowSelection\n    Set objSelection = Document.Selection.CreateRange()\n    Msgbox objSelection.Text\nEnd Sub\n<\/PRE>\n<P>That\u2019s right: just two lines of code. First we create a new <B>TextRange<\/B> object; that\u2019s done by calling the <B>CreateRange()<\/B> method, which happens to be part of the <B>Document.Selection<\/B> object:<\/P><PRE class=\"codeSample\">Set objSelection = Document.Selection.CreateRange()\n<\/PRE>\n<P>As it turns out, one of the properties of the TextRange object is the <B>Text<\/B> property. As the name implies, this property returns the text that happens to be found in this particular TextRange. To return the text currently selected in the HTA all we have to do is display the value of the Text property:<\/P><PRE class=\"codeSample\">Msgbox objSelection.Text\n<\/PRE>\n<P>Incidentally, you aren\u2019t limited to selecting text in a text box or a text area. Select any text within <I>this<\/I> HTA and see what happens:<\/P><PRE class=\"codeSample\">&lt;html&gt;\n&lt;head&gt;\n    &lt;title&gt;HTA Test&lt;\/title&gt;\n&lt;\/head&gt;<\/p>\n<p>&lt;SCRIPT LANGUAGE=&#8221;VBScript&#8221;&gt;\n    Sub ShowSelection\n        Set objSelection = Document.Selection.CreateRange()\n        Msgbox objSelection.text\n    End Sub\n&lt;\/SCRIPT&gt;<\/p>\n<p>&lt;body&gt;\n    &lt;p&gt; Here is some text in an HTA. Select any portion of the text and then\n        click the Show Selection button.&lt;\/p&gt;\n  &lt;input id=runbutton  type=&#8221;button&#8221; value=&#8221;Show Selection&#8221; onClick=&#8221;ShowSelection&#8221;&gt;<\/p>\n<p>&lt;\/body&gt;\n<\/PRE>\n<P>Pretty cool, huh? You know, maybe this <I>is <\/I>the most glamorous and most exciting job in the world.<\/P>\n<P>Nah \u2026.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I determine which text has been selected in an HTA?&#8212; DO Hey, DO. You know, most people think that writing the Hey, Scripting Guy! column every day is one of the most glamorous and exciting jobs in the world. And it probably would be if it wasn\u2019t for the fact [&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-68233","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! How can I determine which text has been selected in an HTA?&#8212; DO Hey, DO. You know, most people think that writing the Hey, Scripting Guy! column every day is one of the most glamorous and exciting jobs in the world. And it probably would be if it wasn\u2019t for the fact [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68233","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=68233"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68233\/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=68233"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68233"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68233"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}