Hey, Scripting Guy! Question

Hey, Scripting Guy! How can I determine which text has been selected in an HTA?

-- DO

SpacerHey, Scripting Guy! AnswerScript Center

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’t for the fact that the Scripting Guy who writes this column is so lazy. For example, lots of times he selects questions that he knows can be scripted. He usually doesn’t have a script on-hand that carries out the task in question, but he knows that it’s possible to write such a script. That’s an efficient way to do things, but not a particularly exciting way to do things.

This question was a little different, however. How can you determine which text has been selected in an HTA? Heck, we weren’t even sure if you can determine which text has been selected in an HTA. But it turns out that you can:

<html>
<head>
    <title>HTA Test</title>
</head>

<SCRIPT LANGUAGE="VBScript">
    Sub ShowSelection
        Set objSelection = Document.Selection.CreateRange()
        Msgbox objSelection.text
    End Sub
</SCRIPT>

<body>
     <textarea name="ScriptArea" rows=5 cols=40></textarea><p>
     <input id=runbutton  type="button" value="Show Selection" onClick="ShowSelection">
</body>

What we have here is a simple little HTA that looks something like this:

HTA


What’s that? How could you not be impressed? Well, try this. Type some text in the text area and then highlight a portion of that text:

HTA


Now click the button labeled Show Selection. With any luck at all you’ll get a message box telling you which text has been selected:

HTA


You’re right: that’s a bit more like it.

So how does this all work? Well, we start off by creating an HTA that includes a text area and a button. We won’t discuss the details behind creating the HTA itself; if you need a little background information check out the HTA Developers Center or our Scripting Week 3 webcast on HTAs. Today we’re just going to focus on the subroutine that runs when you click the button:

Sub ShowSelection
    Set objSelection = Document.Selection.CreateRange()
    Msgbox objSelection.Text
End Sub

That’s right: just two lines of code. First we create a new TextRange object; that’s done by calling the CreateRange() method, which happens to be part of the Document.Selection object:

Set objSelection = Document.Selection.CreateRange()

As it turns out, one of the properties of the TextRange object is the Text 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:

Msgbox objSelection.Text

Incidentally, you aren’t limited to selecting text in a text box or a text area. Select any text within this HTA and see what happens:

<html>
<head>
    <title>HTA Test</title>
</head>

<SCRIPT LANGUAGE="VBScript">
    Sub ShowSelection
        Set objSelection = Document.Selection.CreateRange()
        Msgbox objSelection.text
    End Sub
</SCRIPT>

<body>
    <p> Here is some text in an HTA. Select any portion of the text and then
        click the Show Selection button.</p>
  <input id=runbutton  type="button" value="Show Selection" onClick="ShowSelection">

</body>

Pretty cool, huh? You know, maybe this is the most glamorous and most exciting job in the world.

Nah ….