HTA
Tutorial #9Having described in the last tutorial how to control the window appearance, I will not be including that code in any further tutorials for the sake of clarity. Now we are getting into saving data to a file. This is something that you normally wouldn't be able to do in normal web pages whilst on the Internet. With WEBlications however, all security is switched off so we are free to save to a file on the local hard drive which almost all useful applications will want to do at some point. Here we are creating a file from scratch. If you try and run it twice, the file will be overwritten.
<HTML>
<BODY>
<HEAD>
<title>Save</title>
<HTA:APPLICATION>
<Script Language=VBS>
sub subWrite_File()
dim b
dim fs
Set fs = CreateObject("Scripting.FileSystemObject")
Set b = fs.CreateTextFile("hello.txt", True)
b.WriteLine("Hello")
b.Close
end sub
</SCRIPT>
</HEAD>
<body scroll="no">
<INPUT TYPE="Submit" ID="btnQuit" VALUE="Quit App" onclick=self.close()>
<INPUT TYPE="Submit" ID="Create" VALUE="Create" onclick=subWrite_File()>
</BODY>
</HTML>
And here is what it all means.
<HTML>
<BODY>
<HEAD>
<title>Save</title>
<HTA:APPLICATION>
<Script Language=VBS>
This is the same as usual up to this point.
sub subWrite_File()
The new subroutine starts here
dim b
dim fs
We first create (or DIMension) some variables
that we will use further down.
Set fs = CreateObject("Scripting.FileSystemObject")
The variable fs
is assigned the object which gives us access to the file system
on the hard drive using the keyword set which is
used for all object assignment to differentiate it from ordinary
variables. An object is simply a piece of code that can do things
for us.
Set b = fs.CreateTextFile("hello.txt", True)
Now it gets slightly complicated.
Again we set the variable b to
point to the file system which is now held in the variable fs.
With fs we can then access all of the routines (methods)
held within the file system object one of which is the one to
create a text file. It might take you a while to get your head
around what these two lines of code is doing. We first create an
object that happens to be the file system object. Using this
reference, we can then get to the routine that will create the
file for us which in this case we have called "hello.txt"
which will be written to the folder where the .HTA file is stored.
b.WriteLine("Hello")
Here we are using the reference b
to get to the routine that will write a line into the file object
created above with the simple string hello.
b.Close
Now its all downhill. We close the file so it cannot be written
to.
end sub
And we end the subroutine.
</SCRIPT>
</HEAD>
<body scroll="no">
That ends the script scetion and
we switch off scrolling of the window as in the last tutorial.
<INPUT TYPE="Submit" ID="btnQuit"
VALUE="Quit App" onclick=self.close()>
Now we come to the HTML tags
that in this case will create a button called Quit
and the onclick command will simply close the
window just as if it was a real application. It can of course
still be closed down in the usual way using the cross button in
the top right hand corner of the window in the caption.
<INPUT TYPE="Submit" ID="Create"
VALUE="Create" onclick=subWrite_File()>
</BODY>
</HTML>
Clicking on the Create button will cause the subWrite_File
subroutine to be run which will create the file and put the
string hello in it. This routine has to be used
sparingly as it overwrites the file when run which in a real
application might overwrite existing data which might not be what
you intended. In a later tutorial, I'll show you how to get
around these problems.