ask:
Hello, script expert! How to determine which text is selected in HTA?
--DO
answer:
Hello DO. You know, most people think that writing a “Hello, script expert!” column every day is one of the most attractive and exciting jobs in the world. Maybe it is if the scripting experts who write this column aren't so lazy. For example, many times he chooses a problem that he thinks can be written into a script. Usually he doesn't have a script on hand that can complete the tasks mentioned in the question, but he knows that such a script can be written. This is a proven method, but not particularly exciting one.
However, this question is a bit different. How can you determine which texts are selected in the HTA? Alas, we can't tell if you can determine which texts you selected in HTA. However, it turns out that you can be sure:
<html>
<head>
<title>HTATest</title>
</head>
<SCRIPTLANGUAGE="VBScript">
SubShowSelection
SetobjSelection=Document.Selection.CreateRange()
MsgboxobjSelection.text
EndSub
</SCRIPT>
<body>
<textareaname="ScriptArea"rows=5cols=40></textarea><p>
<inputid=runbuttontype="button"value="ShowSelection"onClick="ShowSelection">
</body>
We are using a small, simple HTA here, which looks like this:
What? How can you not have an impression? So, give it a try. Type some text in this text area and highlight a portion of the text:
Now click the button labeled Show Selection. Fortunately, you will see a message box telling you which texts have been selected:
You are right: That's what it looks like.
So, how is all this done? We first create an HTA with a text area and a button. We don't discuss the details of creating an HTA itself, if you need some background information, check out the HTA Developers Center or Scripting Week 3 webcast on HTAs. Today we will only focus on the subroutines you run when you click this button:
SubShowSelection
SetobjSelection=Document.Selection.CreateRange()
MsgboxobjSelection.Text
EndSub
That's right: there are only two lines of code. First we create a new TextRange object; this process can be implemented by calling the CreateRange() method, which happens to be part of the Document.Selection object: