How does VBS determine which text is selected in an HTA? _hta

Source: Internet
Author: User
Ask:
Hello, Scripting Guy! How do I determine which text is selected in an HTA?
--Do
For:
Hello, do. You know, most people think that writing every day "Hello, Scripting Guy!" "The column is one of the most fascinating and exciting jobs in the world," he said. If the Scripting Guys who write this column are not so lazy, maybe so. For example, many times he chooses a question he thinks can be scripted. Usually he does not have the script to complete the task mentioned in the question, but he knows he can write such a script. This is an effective method, but not a particularly exciting one.
However, the problem is somewhat different. How can you determine which text is selected in an HTA? Gee, we're not sure if you can determine which text is selected in the HTA. However, it turns out that you can determine:
<title>hta test</title>
<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're using here is a small, simple HTA with the following look:


What the? How can you not have the impression? Well, give it a try. Type some text in the text area, and then highlight part of the text:


Now click the button labeled Show Selection(display selection). Luckily, you'll see a message box that tells you which text has been selected:


You're right: that's what it looks like.

So how did it all work out? We first create an HTA that contains a text area and a button. We don't discuss the details of creating the HTA itself, and if you need some background information, check outHTA Developers Center(English) orScripting Week 3 Webcast on HTAsEnglish. Today we only focus on the subroutine that runs when you click this button:
Sub ShowSelection
Set objselection = Document.Selection.CreateRange ()
Msgbox Objselection.text
End Sub
Yes: only two lines of code. First we create a new TextRange object that can be implemented by calling the Createrange () method, which happens to be part of the Document.selection object:
Set objselection = Document.Selection.CreateRange ()
It turns out that one of the properties of the TextRange object is the Text property. As the name suggests, the text returned by this property is exactly the text 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
By the way, the selected text is not limited to text in a text box or text area. Select any text within this HTA to see what happens:
<title>hta test</title>
<script language= "VBScript" >
Sub ShowSelection
Set objselection = Document.Selection.CreateRange ()
Msgbox Objselection.text
End Sub
</SCRIPT>
<body>
<p> 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 it's the most fascinating and exciting job in the world.
All right....

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.