by Jack Wheeler and Dr. Peter Wayne
| Using the Windows Scripting Host® and VBScript®, you can obtain system information, manipulate system parameters, and even call MS Office® components, all from within your application's control. |
A little-known feature of Windows is the Windows Scripting Host, an interpreter of the VBScript language. One or another version of the Windows Scripting Host has been provided with Windows ever since Windows 98. You can download the most recent version of Windows Script (version 5.6 at the time of this writing) from Microsoft at http://www.microsoft.com (enter "windows script" in Microsoft's search box and you will be taken to the latest version.)
VBScript is a subset of Visual Basic. A VBScript program is a text file ending in .vbs that can be run by the Windows Script interpreter. The Windows Script interpreter can manipulate many of your system's files, add and delete registry keys, read (with appropriate hooks) your database files, send email--indeed, malicious VBScript programs are a favorite mechanism for virus propagation. So be careful with your VBScripts!
The simplest VBScripts look very similar to Xbasic scripts. Open Notepad and save this as a text file, "hello.vbs"
| msgbox "Hello there from VBScript" |
Script 1. Hello.vbs
Click on the file in Windows Explorer, and Windows Script will open and run the file:
Fig. 1. Output of simple hello.vbs
More complex VBScripts can access system parameters and change them. For example, this script lets you change the default Windows printer. This script was taken from www.swynk.com, a good source for free sample scripts:
'*****************************************************
' This WSH script allows the user to quickly set
' the default printer.
'*****************************************************
Option Explicit
Dim Text, Title, i, j, tmp, printer, PrinterText
Dim WshNetwork, oDevices
Title = "SWYNK.com"
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oDevices = WshNetwork.EnumPrinterConnections
Text = "Listed below are the printers currently available to you. Please
enter the number of the printer you want to set as the default." &_
vbCrLf & vbCrLf
j = oDevices.Count
For i = 0 To j - 1 Step 2
Text = Text & (i/2) & vbTab
Text = Text & oDevices(i) & vbTab & oDevices(i+1) & vbCrLf
Next
tmp = InputBox(Text, "Select default printer", 0)
If tmp = "" Then
WScript.Echo "No user input, aborted"
WScript.Quit
End If
tmp = CInt(tmp)
If (tmp < 0) Or (tmp > (j/2 - 1)) Then
WScript.Echo "Wrong value, aborted"
WScript.Quit
End
If printer = oDevices(tmp*2 + 1)
WshNetwork.SetDefaultPrinter printer
MsgBox "Your default printer has been successfully set to " &printer, _
vbOKOnly + vbInformation, Title
'***
End
|
Script 2. PrinterSelect.vbs

Fig. 2. Printer selection.
You can call this VBScript from within Alpha Five by simply writing
| dir_put(a5.get_path()) sys_shell("wscript PrinterSelect.vbs") |
My assumption here is that you have put PrinterSelect.vbs in the same directory as your application. If you put it in another directory, such as the \codes subdirectory of your application, change the first line above to
| dir_put(a5.get_path()+"\codes") |
You can do more than just execute VBScripts externally of Alpha Five. VBScript can read and write to your .dbf files through ODBC. For this to work, you have to set ODBC up properly on your computer. On my Windows 98 system, I went to Settings, Control Panel, ODBC (32 bit), and opened this window:

Fig. 3. Open the ODBC configuration pane in the Contrl Panel.
I then pressed "Add" and chose the Microsoft Foxpro Driver:
Fig. 4. Choose the Microsoft Foxpro driver.
To configure the driver, I set it up as a USER data source:

Fig. 5. Make sure you choose Foxpro 2.6 as the Version.
I then unchecked the box saying Use Current Directory and selected the directory in which I put the application's tables--in this case, c:\program files\a5v4\wheeler:

Fig. 6. Choose the directory for the USER data source.
Now that ODBC is properly set up, VBScript can read and write to our .dbf files.
Next, we created a one-record table to store system parameters. The structure of the table is:

Fig. 7. The table continues with Prt7name up to Prt15name, to allow storing up
to 15 printers in this record.
When the database starts, the autoexec script executes a VBScript, "profile.vbs", that reads all the system parameters, empties the user.dbf, and updates user.dbf with the proper system parameters (user name, windows default directory, computer name, and printer connections). Here is the Alpha Five autoexec script:
''XBasic
dim code_file as c
dir_put(a5.get_path()+"\codes")
code_file="profile.vbs"
sys_shell("wscript "+code_file)
form.LOAD("mainmenu")
:mainmenu.show()
:mainmenu.restore()
:mainmenu.activate()
|
Script 3. This autoexec script runs a VBScript named profile.vbs in the \codes subdirectory under the application's folder, and then opens the MainMenu form.
The profile.vbs script is a fairly sophisticated VBScript that reads environment variables from Windows, opens user.dbf through ODBC, and then updates user.dbf:
dim RS, MainMenu,objConn, shell, WshNetwork
dim oprinters, env, strprnt, strprntnme, maxprnt, i, n
set shell = Wscript.createobject("Wscript.shell")
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set oPrinters = WshNetwork.EnumPrinterConnections
Set objConn = CreateObject("ADODB.Connection")
objconn.open "user"
set RS = createobject("Adodb.Recordset")
set mainmenu = CreateObject("Adodb.RecordSet")
Rs.open "select * from user",objconn,,3
MainMenu.open "Select * from MainMenu",objconn,,3
' the next On Error is required in case user.dbf is empty
on error resume next
rs.movefirst
while Not Rs.eof
rs.delete
rs.update
rs.movenext
wend
set env = shell.Environment
mainmenu.fields("dummy")=wshNetwork.username
mainmenu.update
rs.addnew
rs.fields("user")=wshNetwork.username
rs.fields("computer")=WshNetwork.computername
rs.fields("temp")=env("temp")
rs.fields("windir")=env("windir")
rs.fields("Processor")="four"
if oPrinters.Count > 30 then
maxprint=30
else
maxprint=oprinters.count
end if
For i = 0 to maxprint -1 Step 2
n=int(i/2)+1
strprnt="prt" & n
strprntnme="Prt"&n&"name"
'msgbox "oPrinters.Item(i) is " & oPrinters.Item(i)
rs.fields(""&strprnt&"")=oPrinters.Item(i)
rs.fields(""&strprntnme&"")=oPrinters.Item(i+1)
Next
rs.update
rs.close
mainmenu.close
objconn.close
|
Script 4. The profile.vbs VBScript.
This is not a tutorial in VBScript, so I'm not going into detail about how profile.vbs works. The only clarification I want to make is that the line,
| objconn.open "user" |
refers to the USER ODBC connection, not to the user.dbf table. The later lines,
set RS = createobject("Adodb.Recordset")
Rs.open "select * from user",objconn,,3
|
do reference user.dbf. I apologize for the confusion in naming the table "user".
The system parameters can now be viewed in Alpha Five:
Fig. 8. System parameters are now in an Alpha Five table. Notice that the
computer's name and the computer user's login name are now part of an A5 table
and accessible to any A5 program that needs them!
As a last exercise, I will show how we can change the default printer from within A5. All we have to do is crib a little from Script 2 and use a little Xbasic to read the printer names from user.dbf:
''XBasic
' originally by Jack Wheeler, modified by PKW
dim choice as c
dim prn[15] as c
dim t as p
t= table.open("User")
for i = 1 to 15
prn[i]=rtrim(eval("t.prt"+alltrim(str(i))))+eval("t.prt"+alltrim(str(i))+"name")
next
choice=ui_get_list_array("Default Printer Choice",1,"prn")
substr(choice,1,at("-",choice,1)-1)
if choice="" then
end
else
t.change_begin()
t.Olddefault=t.defaultprt+" "+t.defprtnme
t.defaultprt=substr(choice,1,at("-",choice,1)-1)
t.defprtnme=right(choice,(len(choice)-len(substr(choice,1,at(":",choice,1)))))
t.change_end(.t.)
t.close()
end if
dir_put(a5.get_path()+"\codes")
fn="setprnt.vbs"
sys_shell("wscript "+fn)
|
Script 5. Alpha Five script to choose a printer.
Pressing the "Set Default Printer" button runs Script 5 and produces this output:

Fig. 9. Printer selection from A5.
Once you have chosen a printer from within A5, the VBScript, setprnt.vbs, sets the default printer in Windows:
' setprnt.vbs
' by jack wheeler, modified pkw
' reads selected printer from user.dbf and changes the Windows default printer
dim rs,objConn, wshnetwork,default, location
Set WshNetwork = WScript.CreateObject("WScript.Network")
Set objConn = CreateObject("ADODB.Connection")
objconn.open "user"
set RS = createobject("Adodb.Recordset")
Rs.open "select * from user",objconn,,3
RS.MOVEFIRST
default = trim(Rs("defprtnme"))
location = trim(Rs("defaultprt"))
Wshnetwork.setdefaultprinter ""&default&""
rs.close
objconn.close |
Script 6. setprnt.vbs is called from within A5 to change the default Windows printer.
Jack has done many other nifty things with VBScript and A5v4. He has developed ways to call Outlook® through A5, to populate Excel® tables, to read and write to the Windows registry, and to call the MS Office spellchecker. I will not reproduce all of his work here, because
Nevertheless, I am sure that there are many features of VBScript that will not be present in A5v5, and Jack has certainly shown us a way to grab some of that enormous volume of public domain and published VBScript code and incorporate it into Alpha Five. You can reach Jack at jwl13@socal.rr.com
6/16/02 - pkw
Don't forget, we need your feedback to make this site better!