c-- styles for logos and headline links do not modify internet, red, or black styles -->

Intranet Journal   Earthweb  
Events Jobs Premium Services Media Kit Network Map E-mail Offers Vendor Solutions Webcasts

   Intranet Journal Subjects
Search Earthweb

Privacy Policy



internet.com
IT
Developer
Internet News
Small Business
Personal Technology

Search internet.com
Advertise
Corporate Info
Newsletters
Tech Jobs
E-mail Offers

internet commerce
Be a Commerce Partner
















 

[ Home | Discussion Forum | How Do I... | Lotus Notes Intranets | Microsoft SharePoint | Products | Shopping  ]

free news!

VBScript in a Nutshell


 

VBScript in a Nutshell

By Paul Lomax, Matt Childs & Ron Petrusha


Chapter Excerpt: VBScript Program Structure

  • Introduction
  • Defining Subroutines: The Sub . . . End Sub Construct
  • Calling a Subroutine
  • Passing Variables into a Subroutine
  • Exiting a Routine with the Exit Statement
  • The Class Construct, Variables, Properties, Methods and Events
  • The Script Level: Active Server Pages
  • Windows Script Host
  • Client-Side Scripts for MSIE
  • Outlook Forms
  • Reusable Code Libraries: Active Server Pages
  • Reusable Code Libraries: Windows Script Host
  • Reusable Code Libraries: Client-Side Scripts for MSIE

    Printer Friendly Version

    Reusable Code Libararies-Windows Script Host

    Although standard Windows Script host files (i.e., .vbs files) do not allow you to import other files, WSH files with XML elements (i.e., .wsf files) do. Include another file by using the <SCRIPT SRC> tag, the syntax of which is:

    <SCRIPT LANGUAGE="sLanguage" SRC="sFilename" />
    

    where sLanguage is "VBScript" (or any other valid scripting language) and sFileName is either an absolute or a relative path to the file to be excluded. Note that using the <SCRIPT> tag requires that the .wsf file be structurally correct--that is, that the <PACKAGE> and <JOB> tags should be present.

    The included file must be a standard WSH script file. It can contain only script, without any XML elements or tags. The include file is simply inserted into the .wsf file as if it were an intrinsic part of it.

    Examples 2-12 and 2-13 illustrate the use of an include file. In this case, the code in Example 2-13 imports Lib.vbs, the include file shown in Example 2-12. Example 2-12 simply displays a message box displaying drives and their free space. To retrieve this information, it calls the GetFreeSpace function, which is located in the include file. This function returns a Dictionary object whose keys are drive names and whose values are the amount of free space available on the respective drive.

    Example 2-12: Lib.vbs, an Include File
    <%
    Public Function GetFreeSpace(  )
     
    Dim oDict, oFS, oDrives, oDrive
     
    Set oDict = WScript.CreateObject("Scripting.Dictionary")
    Set oFS = WScript.CreateObject("Scripting.FileSystemObject")
    Set oDrives = oFS.Drives
    For Each oDrive in oDrives
       If oDrive.IsReady Then
          oDict.Add oDrive.DriveLetter, oDrive.FreeSpace
       End If
    Next
     
    Set GetFreeSpace = oDict
     
    End Function
    %>
    

    Example 2-13: A WSH Script That Uses an Include File

    <PACKAGE>
    <JOB ID=GetFreeSpace>
    <SCRIPT LANGUAGE="VBScript" SRC="Lib.vbs" />
    <SCRIPT>
    Option Explicit
     
    Dim oSpace, aDrives
    Dim sMsg, sDrive
    Dim iCtr
     
    Set oSpace = GetFreeSpace(  )
    aDrives = oSpace.Keys
    for iCtr = 0 to UBound(aDrives)
       sDrive = aDrives(iCtr)
       sMsg = sMsg & sDrive & ": " & oSpace(sDrive) & vbCrLf
    next
     
    MsgBox sMsg
    </SCRIPT>
    </JOB>
    </PACKAGE>>
    

    Note that files must be included on a per-job basis. In other words, if a .wsf file contains multiple jobs, you must have a separate <SCRIPT SRC> tag for each job in which you want to include a particular file. An include file applies only to the job in which it's been included.

    Client-Side Scripts for MSIE

    Like Windows Script Host, MSIE supports the <SCRIPT SRC> tag, which allows you to include script files. The syntax of the tag is:

    <SCRIPT SRC="sURL " LANGUAGE="sLanguage"> </SCRIPT>
    

    where sURL is the URL of the include file and sLanguage is the language in which the file designated by sURL is written. sLanguage can be "VBScript" or any other valid scripting language.

    The include file is simply inserted into the text stream on the client at the point that the <SCRIPT SRC> tag is encountered, and both it and the original document are viewed by the VBScript language engine as a single document. The inserted file can contain only script, without any HTML tags.

    Example 2-14 contains an include file and Example 2-15 an HTML document that includes a client-side script to validate data. Note that the IsBlank routine is visible to the web page, since the included script is considered part of the original document. Note also that Validate.inc contains only script, without any HTML tags, and that the source document contains a <SCRIPT SRC> tag immediately followed by a </SCRIPT> tag.

    Example 2-14: Validate.inc, an Include File
    Private Function IsBlank(sValue)
       If Trim(sValue) = "" Then
          IsBlank = True
       Else
          IsBlank = False
       End If
    End Function
    

    Example 2-15: A Web Page That Uses an Include File

    <HTML>
    <HEAD>
    <TITLE>The SRC Attribute</TITLE>
    <SCRIPT SRC="Validate.inc" LANGUAGE="VBScript" > </SCRIPT>
    </HEAD>
    <BODY>
    <SCRIPT LANGUAGE="VBScript">
    Private Function frmInfo_OnSubmit(  )
       Dim oFrm
       Set oFrm = Document.frmInfo
       If IsBlank(oFrm.txtName.Value) Or _
          IsBlank(oFrm.txtAddress.Value) Or _
          IsBlank(oFrm.txtCity.Value) Or _
          IsBlank(oFrm.txtState.Value) Then
             frmInfo_OnSubmit = False
             Alert "Please make sure the Name, Address, City " & _
                   "State fields are not blank."
       End If
    End Function
    </SCRIPT>
    <H3>Please enter the following data</H3>
    <FORM METHOD=POST ACTION="Submission.asp" NAME="frmInfo">
    Name: <INPUT TYPE="Text" NAME="txtName"> <P>
    Address: <INPUT TYPE="Text" NAME="txtAddress"> <P>
    City: <INPUT TYPE="Text" NAME="txtCity">
    State <INPUT TYPE="Text" NAME="txtState">
    Zip Code <INPUT TYPE="Text" NAME="txtZip"><P>
    <INPUT TYPE="submit">
    </BODY>
    </HTML>
    

    Printer Friendly Version

  • Introduction
  • Defining Subroutines: The Sub . . . End Sub Construct
  • Calling a Subroutine
  • Passing Variables into a Subroutine
  • Exiting a Routine with the Exit Statement
  • The Class Construct, Variables, Properties, Methods and Events
  • The Script Level: Active Server Pages
  • Windows Script Host
  • Client-Side Scripts for MSIE
  • Outlook Forms
  • Reusable Code Libraries: Active Server Pages
  • Reusable Code Libraries: Windows Script Host
  • Reusable Code Libraries: Client-Side Scripts for MSIE

  • Of Interest
    · The Elements of Intranet Style
    · A Tutorial in VBScript