Sub cmdVariables_OnClick
Dim Name
Name = InputBox("Enter your name: ")
MsgBox "The name you entered was " & Name
End Sub
Dim TAX_RATE
TAX_RATE = .06
Function CalculateTaxes
CalculateTaxes = CostOfGoods * TAX_RATE
End Function
ARRAYS
Dim States(50)
States(5) = "California"
States(6) = "New York"
Arrays can have multiple dimensions, VBScript supports up to 60. Declaring
a two dimensional array for storing 51 states and their capitals could be
done as follows:
Dim StateInfo(50,1)
To store values into this array you would then reference both dimensions.
StateInfo(18,0) = "Michigan"
StateInfo(18,1) = "Lansing"
VBScript also provides support for arrays whose size may need to change as
the script is executing. These arrays are referred to as dynamic arrays.
A dynamic array is declared without specifying the number of elements it
will contain:
Dim Customers()
The ReDim statement is then used to change the size of the array from within
the script:
ReDim Customers(100)
There is no limit to the number of times an array can be re-dimensioned
during the execution of a script. To preserve the contents of an array when
you are re-dimensioning, use the Preserve keyword:
ReDim Preserve Customers(100)
If AmountPurchased > 10000 Then
DiscountAmount = AmountPurchased * .10
Subtotal = AmountPurchased - DiscountAmount
Else If AmountPurchased > 5000 Then
DiscountAmount = AmountPurchased * .05
Subtotal = AmountPurchased - DiscountAmount
Else
HandlingFee = AmountPurchased *.03
Subtotal = AmountPurchased + HandlingFee
End If
Select Case Document.frmOrder.txtState.Value
Case "California"
ShippingFee= .04
Case "Florida"
ShippingFee = .03
Case Else
ShippingFee = .02
End Select
' This script runs a command and kills the process after 5 seconds if it is still running
Option Explicit
Dim objWScript, objExec
Dim intCount
'Create the WScript shell object
Set objWScript = CreateObject("WScript.Shell")
'Create a constant with the command to execute
Const strLaunchCmd = "notepad"
'Run the command
Set objExec = objWScript.Exec(strLaunchCmd)
'Set the timeout counter to zero
intCount = 0
'Loop for 5 seconds or until objExec status = 0 (success)
Do While intCount < 500
'Exit the loop if the status is not zero
If objExec.Status <> 0 Then
Exit Do
End If
intCount = intCount + 1
WScript.Sleep 10
Loop
'Terminate the process if it is still running (status = 0)
If objExec.Status = 0 Then
WScript.Echo vbCrLf & "Terminating Process!" & vbCrLf
objExec.Terminate
End If
' Requires Microsoft Word 97 or higher installed
' Comments to alexangelopoulos@hotmail.com
' save as wordcheck.vbs
' files can also be dropped onto the script to spellcheck them. ' The error and optional completion messages are implemented with ' wscript.echo; this allows the script to work correctly even if ' cscript is chosen as the engine for execution. ' Inspired by a spelling-only implementation of an ASP-based checker ' originally done by Anil Parangat.
' Check that 1 and only 1 argument is supplied on the command line ' If not, then exit ' this allows correct operation if used externally Set objArgs = WScript.Arguments if objArgs.length <> 1 Then wscript.echo "Supply the file name as 1 argument." : wscript.quit
' Step 1: Read in the TextPad file
strcheckText = strInFileText(objArgs(0))
'Step 2: Fire up the word components and check the text strCheckText = Spelling_Grammar(strCheckText)
'Step 3: Save it back to the file.
WriteFile objArgs(0),strCheckText
'If you want final notification, uncomment the following line 'wscript.echo "Spelling and grammar check of " & objArgs(0) & " done."
Function strInFileText(strFilePath)
'Given the path to a VBScript file, subImportCodeFile will
'import it and execute it globally
Set fso = CreateObject("Scripting.FileSystemObject")
CONST ForReading = 1, ForWriting = 2
Dim fso, f
'get Text file read in
Set f = fso.OpenTextFile(strFilePath, ForReading)
strInFileText = f.ReadAll
End Function
Sub modWriteToFile(strToWrite,strFilePath)
'strToWrite is data written to file strFilePath
CONST ForReading = 1, ForWriting = 2, ForAppending = 8
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set fileRef = objFSO.OpenTextFile(strFilePath, ForAppending, True)
fileRef.WriteLine(strToWrite)
End Sub
function Spelling_Grammar(TextValue)
Dim objWord, objDocument, strReturnValue
Set objWord = CreateObject("word.Application")
objWord.WindowState = 2
objWord.Visible = False
'Create a new instance of Document
Set objDocument = objWord.Documents.Add( , , 1, True)
objDocument.Content=TextValue
objDocument.CheckSpelling
objDocument.CheckGrammar
'Return checked text and quit Word
strReturnValue = objDocument.Content
objDocument.Close False
objWord.Application.Quit True
Spelling_Grammar=strReturnValue
End function
Sub WriteFile(strFilePath,strData)
'Given the path to a VBScript file, subImportCodeFile will
' import it and execute it globally
Set fso = CreateObject("Scripting.FileSystemObject")
CONST ForReading = 1, ForWriting = 2
Dim fso, f
'get Text file read in
Set f = fso.OpenTextFile(strFilePath, ForWriting)
f.Write strData
End Sub
' which opens a command prompt (DOS) window in the selected folder
Dim WSHShell
Set WSHShell = WScript.CreateObject("WScript.Shell")
WSHShell.RegWrite "HKCR\Folder\Shell\MenuText\Command\", "cmd.exe /k cd " & chr(34) & "%1" & chr(34)
WSHShell.RegWrite "HKCR\Folder\Shell\MenuText\", "Cmd Prompt Here"