Searching Body Text with textRange: Building on the Script and the VBScript Alternative - The VBScript
(Page 3 of 4 )
As promised, I will now give you the VBScript variation of the whole script. Some may question what the point of using VBScript actually is when the JavaScript version does everything that is needed from it? I’m including this s o that people can choose whichever version they use depending on whichever language they prefer. Additionally, as VBScript is basically a cut-down version of Visual Basic, learning VBScript is an excellent primer for those wishing to progress to application programming with full VB or even VB.NET. Those that don’t want to know should look away now.
The VBScript file in its entirety then is as follows. I won’t go through the code step by step because you should already be able to tell what is going on in the file:
dim searchWord
dim range
dim match
dim button1clicked
sub buttonSearch_onclick()
button1clicked = 1
set range = document.body.createTextRange()
searchWord=window.showModalDialog("Searchwindow.htm",
null,"dialogWidth:300px;dialogHeight:200px;status:no;help:no;")
if searchWord="" then
exit sub
end if
match = range.findText(searchWord,0,2)
if match <> false then
range.select()
else
MsgBox "Couldn't find " & searchWord,vbExclamation,"No Match"
end if
end sub
sub buttonNext_onclick()
if searchWord="" then
MsgBox "No search value entered",vbCritical, "No Search Word"
exit sub
end if
if button1clicked <> 1 then
MsgBox "Please enter a search value first", vbCritical, "No Search Word"
exit sub
end if
range.move "word", 1
range.moveEnd "word", 1000
match = range.findText(searchWord,0,2)
if match <> false then
range.select()
else
MsgBox "Couldn't find any more instances of " & searchWord,vbExclamation,"No Further Matches"
end if
end sub
Initially, the global variables are defined using the dim keyword instead of the var keyword. This is standard practice in VBScript. Next up, although VBScript does support the use of functions, this script has made use of subs instead.
Next: VBScript Continued >>
More Code Examples Articles
More By Dan Wellman